Differences between rel1 and rel3

Martin Wuerthner martin at mw-software.com
Fri Sep 12 06:40:48 PDT 2003

In message <bba96e2e4c.philip at philipnet.com>
          Philip Ludlam <philip at philipnet.com> wrote:

> I can't remember if the standard says that with:
>   data_desc d[2];
> that:
>   &(d[1]) == &(d[0]) + sizeof data_desc;
> (that the address of the 2nd element of an array == address of the 1st
> element + size of the array type).

Yes, it does, but you have to put the assertions from various parts of the
standard together.

The standard (ANSI C 89) asserts:
- if p points into a vector, then p+1 points to the next element (A7.7)
- for p+n, where p is a pointer and n an integer, the value n is
  multiplied by the size of the object pointed to by p, i.e., p+n yields
  an address p+n*(size of pointer target type) (A7.7)

If we wanted to be pedantic, then so far, it does NOT assert that "size of
pointer target type" is identical to sizeof(type), at least not in this
section, so to just satisfy A7.7, a compiler could choose a larger
distance between elements. However, fortunately, you can infer the
equality from A7.4.8 where it states that sizeof(type) yields the number
of bytes required to store an element of type "type" including the padding
required to allow this member to be part of a vector. It then goes on to
say that a vector of n elements has an overall size of n*the member size.

Martin Wuerthner          MW Software          martin at mw-software.com

More information about the gcc mailing list