c/9058: structure with flexible array member: offsetof() != sizeof()

Manfred Spraul manfred@colorfullife.com
Thu Dec 26 13:45:00 GMT 2002


Joseph S. Myers wrote:

>On 26 Dec 2002 manfred@colorfullife.com wrote:
>
>  
>
>>Now sizeof(struct x) is 8, offsetof(struct x, c) is 6. That seems to
>>contradice the C99 standard: section 6.7.2.1, constraint 16.
>>    
>>
>
>Please read the suggested TC in response to DR 282.  (See N983
><http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n983.htm> and the Santa Cruz
>minutes <http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n987.htm> - the DR
>log hasn't yet been updated.)
>
Thanks, I found the thread "C99 conformance bug in gcc-3.1" after filing 
the bug report.
What's the recommended approach to calculate the size for malloc calls? Is

    struct a {int a; char b; short c[]; };

    len = offsetof(struct a, c[nr_entries]);

valid and portable?

    sizeof(struct a)+nr_entries*sizeof(short);

would waste 2 bytes.

--
    Manfred



More information about the Gcc-bugs mailing list