This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C99 conformance bug in gcc-3.1

>>>>> "Fergus" == Fergus Henderson <> writes:

Fergus> I think this is a defect in C99.  C99 does not match existing practice
Fergus> here.  I note that Sun C, Compaq C, and GNU C all use the same layout
Fergus> for this struct.  This layout is more efficient (uses less space) than
Fergus> the layout mandated by C99.  I don't think the committee intended to
Fergus> force implementations to be inefficient in this way and I don't think the
Fergus> committee intended to force implementations to break binary compatibility
Fergus> with previous releases.

Fergus> I will take it up with WG14.

As far as I can read through[#16] the standard actually
_allows_ for more space efficient layout for structures with flexible
array members by recognizing that this kind of structures are usually
not put in arrays, thus there's no need to align the size of the
structure to the maximum member alignment.

Of course, there's a potential problem if one indeed puts the
structure in array. 

Is the following equality required by the standard:

struct foo a[DIM];

(char *) &a[i] == (char *) &a[i-1] + sizeof (struct foo) ?

Fergus> On 30-May-2002, Tony Finch <> wrote:

>> $ cat test.c 
>> #include <stddef.h>
>> #include <stdint.h>
>> #include <stdio.h>
>> struct foo {
>> int32_t a;
>> int16_t b;
>> char pad[];
>> };
>> struct bar {
>> int32_t a;
>> int16_t b;
>> char pad[64];
>> };
>> int
>> main(void)
>> {
>> printf("sizeof(struct foo) = %u\n", sizeof(struct foo));
>> printf("offsetof(struct foo, pad) = %u\n", offsetof(struct foo, pad));
>> printf("offsetof(struct bar, pad) = %u\n", offsetof(struct bar, pad));
>> return(0);
>> }
>> $ gcc test.c
>> $ ./a.out 
>> sizeof(struct foo) = 8
>> offsetof(struct foo, pad) = 6
>> offsetof(struct bar, pad) = 6
>> $

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]