array of structures

  • Follow


Hi! I have struct type:
typedef struct abc {int y;int z;} abcType;
Then I have array of them:
abcType abcArray[100];

The normal way to use this is abcArray[5].y
but a programmer can say " abcArray->y "

How do I prohibit (protect from) a programmer doing this erroneously
(flag a compiler error).

Any other suggestions I'd appreciate it.

Also.....
in normal C++ compilers are the 100 abcType structures in one block of
memory(contiguous even if with holes between elements)?

Thanks.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply toddmarshall2002 6/3/2004 11:25:04 AM

toddmarshall2002@yahoo.com wrote:
 > Hi! I have struct type:
 > typedef struct abc {int y;int z;} abcType;
 > Then I have array of them:
 > abcType abcArray[100];
 >
 > The normal way to use this is abcArray[5].y
 > but a programmer can say " abcArray->y "
 >
 > How do I prohibit (protect from) a programmer doing this erroneously
 > (flag a compiler error).

Use an array wrapper instead of a raw array.  For example, Boost.Array
<http://www.boost.org/libs/array/>.  (This is very lightweight, unlike
std::vector.)

 > Any other suggestions I'd appreciate it.
 >
 > Also.....
 > in normal C++ compilers are the 100 abcType structures in one block of
 > memory(contiguous even if with holes between elements)?

They must be contiguous and there must be no holes between elements
(though there can be holes within elements).  sizeof(abcArray) ==
100 * sizeof(abcType).

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ben 6/4/2004 10:35:17 AM


Hi,

> Hi! I have struct type:
> typedef struct abc {int y;int z;} abcType;
> Then I have array of them:
> abcType abcArray[100];

> The normal way to use this is abcArray[5].y
> but a programmer can say " abcArray->y "

> How do I prohibit (protect from) a programmer doing this erroneously
> (flag a compiler error).

Wrap the array in a class that overloads operator[].

> Any other suggestions I'd appreciate it.

> Also.....
> in normal C++ compilers are the 100 abcType structures in one block of
> memory(contiguous even if with holes between elements)?

Yes.

Greetings,
        Thomas


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Thomas 6/4/2004 10:36:01 AM

In article <c7aadd5c.0406021120.23db9b37@posting.google.com>,
toddmarshall2002@yahoo.com writes
>Hi! I have struct type:
>typedef struct abc {int y;int z;} abcType;
>Then I have array of them:
>abcType abcArray[100];
>
>The normal way to use this is abcArray[5].y
>but a programmer can say " abcArray->y "
>
>How do I prohibit (protect from) a programmer doing this erroneously

How can a user do that erroneously? Until we get to overloaded
operators:

abcArray[n] is equivalent to *(abcArray + n) and abcArray[5].y is
equivalent to (abcArray + n) -> y
>(flag a compiler error).
>
>Any other suggestions I'd appreciate it.

Just share with us your motive then we can understand the problem.

>
>Also.....
>in normal C++ compilers are the 100 abcType structures in one block of
>memory(contiguous even if with holes between elements)?

Not only must the memory be contiguous but there must be no
inter-element padding. I.e. sizeof(abcArray) == sizeof(abcType) * n for:

abcType abcArray[n];


--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Francis 6/4/2004 12:42:13 PM

<toddmarshall2002@yahoo.com> skrev i en meddelelse
news:c7aadd5c.0406021120.23db9b37@posting.google.com...
> Hi! I have struct type:
> typedef struct abc {int y;int z;} abcType;

why not just struct abcType {int y;int z;}? You are using a C-style
notation.

> Then I have array of them:
> abcType abcArray[100];
>
> The normal way to use this is abcArray[5].y
> but a programmer can say " abcArray->y "

Yes. arrays are second-class citizens.
>
> How do I prohibit (protect from) a programmer doing this erroneously
> (flag a compiler error).

You could use a std::vector which give you a lot of other stuff. As an
alernative, you could use some fixed-size vector class - I believe Boost has
one.
>
> Any other suggestions I'd appreciate it.
>
> Also.....
> in normal C++ compilers are the 100 abcType structures in one block of
> memory(contiguous even if with holes between elements)?
Yes. There are no "holes" between elements. The same thing goes for
std::vector.

/Peter


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Peter 6/4/2004 9:24:29 PM

toddmarshall2002@yahoo.com wrote:

> Hi! I have struct type:
> typedef struct abc {int y;int z;} abcType;
> Then I have array of them:
> abcType abcArray[100];
>
> The normal way to use this is abcArray[5].y
> but a programmer can say " abcArray->y "
>
> How do I prohibit (protect from) a programmer doing this erroneously
> (flag a compiler error).

use std::vector<>, and then you won't have a pointer/array duality
problem.

> Any other suggestions I'd appreciate it.
>
> Also.....
> in normal C++ compilers are the 100 abcType structures in one block of
> memory(contiguous even if with holes between elements)?

Yes, the array elements will be contiguous in one block of memory.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Antoun 6/4/2004 9:35:01 PM

toddmarshall2002@yahoo.com wrote
> Hi! I have struct type:
> typedef struct abc {int y;int z;} abcType;
> Then I have array of them:
> abcType abcArray[100];
>
> The normal way to use this is abcArray[5].y
> but a programmer can say " abcArray->y "
>
> How do I prohibit (protect from) a programmer doing this erroneously
> (flag a compiler error).

Education. Your programmers probably understand that the name abcArray
decays into a pointer, so that
     abcArray->y
is the same as
     abcArray[0].y
If they specifically want to access the first element, let them. If they
wanted some other element, that's a bug -- show them how to fix it.

> Any other suggestions I'd appreciate it.

Use a vector instead.
     std::vector<abcType> abcArray;
Now abcArray->y has no meaning. There are also other advantages to this
approach.

> Also.....
> in normal C++ compilers are the 100 abcType structures in one block of
> memory(contiguous even if with holes between elements)?

Yes. The address of each element can be calculated by starting with the
address of the first structure. In fact, pointer arithmetic already
takes this into account; if you add N to the pointer, it actually adds
N*sizeof(abcType) to the underlying pointer, so
     7 + &abcArray[0]
gives the same address as
     &abcArray[7]

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply allan_w 6/5/2004 12:16:01 AM

toddmarshall2002@yahoo.com wrote:
> Hi! I have struct type:
> typedef struct abc {int y;int z;} abcType;

Why beat around the bush when you can simply write:

struct abcType {int y; intz;};
This is more readable and eliminates the unnecesary name "abc".

> Then I have array of them:
> abcType abcArray[100];
>
> The normal way to use this is abcArray[5].y
> but a programmer can say " abcArray->y "

This would mean (*abcArray).y, which is the same as abcArray[0].y

> How do I prohibit (protect from) a programmer doing this erroneously
> (flag a compiler error).

No way to stop a built-in array from being converted to the pointer to
the 1st elem. Maybe you can use std::vector<abcType>. It is not
implicitly converted to  an iterator to the 1st element.
> Any other suggestions I'd appreciate it.
>
> Also.....
> in normal C++ compilers are the 100 abcType structures in one block of
> memory(contiguous even if with holes between elements)?

The language is defined by the standard, which guarantees that
elements of a built-in array, and of a std::vector are in contiguous
memory locations, in the natural order.

> Thanks.

You're welcome

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
To iterate is human, to recurse divine.
-L. Peter Deutsch
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply kprateek88 6/5/2004 12:23:35 AM

toddmarshall2002@yahoo.com wrote:
> Hi! I have struct type:
> typedef struct abc {int y;int z;} abcType;
> Then I have array of them:
> abcType abcArray[100];
>
> The normal way to use this is abcArray[5].y
> but a programmer can say " abcArray->y "
>
> How do I prohibit (protect from) a programmer doing this erroneously
> (flag a compiler error).
>
> Any other suggestions I'd appreciate it.

Do not use C-style arrays.  A vector of structs does
not suffer from that problem.

> Also.....
> in normal C++ compilers are the 100 abcType structures in one block of
> memory(contiguous even if with holes between elements)?

Yes.  In this case (for your struct example), it is quite
unlikely that you will have holes at all, since most likely
the processor's memory alignment will be the same as your
sizeof(int), and so two ints will take a "compact" block
of allocation, with proper alignment.

Carlos
--

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Carlos 6/5/2004 12:30:45 AM

Hi,

toddmarshall2002@yahoo.com wrote:

> typedef struct abc {int y;int z;} abcType;
> Then I have array of them:
> abcType abcArray[100];
>
> The normal way to use this is abcArray[5].y
> but a programmer can say " abcArray->y "
>
> How do I prohibit (protect from) a programmer doing this erroneously
> (flag a compiler error).

std::vector<abcType> abcArray(100);

> Also.....
> in normal C++ compilers are the 100 abcType structures in one block of
> memory(contiguous even if with holes between elements)?

The normal array and vector guarantee that all elements will be stored
in a contiguous memory block. Nevertheless, there may be some padding
between the elements, if this is necessary to achieve required alignment.
Consider:

struct abc
{
      int i;
      char c;
};

On a machine where sizeof(int) == 4, there will be very likely (but
that's not required, it is only allowed and typical on popular machines)
3 bytes of padding after the character member. This padding will be
present in every struct object, including those in arrays.
But in regular code you should not bother about such details.

--
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Maciej 6/5/2004 12:35:09 AM

> The normal way to use this is abcArray[5].y
> but a programmer can say " abcArray->y "
>
> How do I prohibit (protect from) a programmer doing this erroneously
> (flag a compiler error).

Using raw arrays, you can't.  An array can implicitly convert to a
pointer to the first element, and the arrow operator will always work
on pointers.

You can, however, replace your array with an std::vector, and now it's
inside a C++ class whose interface closely resembles a raw array, but
memory management is handled by the library code automatically.

> Also.....
> in normal C++ compilers are the 100 abcType structures in one block of
> memory(contiguous even if with holes between elements)?

For arrays, yes.  Otherwise pointer arithmetic would not work.  (The
bracket operator really just calculates the targe address by an offset
from the base address of the array.)

Chris

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply chris 6/5/2004 3:21:41 AM

toddmarshall2002@yahoo.com wrote:

  > Hi! I have struct type:
  > typedef struct abc {int y;int z;} abcType;

This C-style definition is obsolete and unnecessary in C++.

     struct abc {int y; int z;};

is perfectly okay.

  > Then I have array of them:
  > abcType abcArray[100];
  >
  > The normal way to use this is abcArray[5].y
  > but a programmer can say " abcArray->y "
  >
  > How do I prohibit (protect from) a programmer doing this erroneously
  > (flag a compiler error).

By using std::vector<abc>.

  > Any other suggestions I'd appreciate it.
  >
  > Also.....
  > in normal C++ compilers are the 100 abcType structures in one block of
  > memory(contiguous even if with holes between elements)?

Yes. 8.3.4 guarantees this. 5.3.3 gurantees that there are no holes
between elements, i.e. the size of an array of n elements is n times the
size of one element. Elements of struct or class type may have internal
padding, however.

The latest revision of the C++ standard also guarantees contiguous
storage for std::vector.

-- 
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Gerhard 6/5/2004 11:02:43 AM

11 Replies
81 Views

(page loaded in 0.109 seconds)

5/23/2013 3:54:18 AM


Reply: