This is the mail archive of the gcc@gcc.gnu.org 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


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

I will take it up with WG14.

On 30-May-2002, Tony Finch <dot@dotat.at> wrote:
> The web pages about C99 conformance in gcc list "flexible array members"
> as "done" (with one caveat about constraint checking) but there is a
> bug in the implementation. Section 6.7.2.1 paragraph 15 says "the size
> of the structure shall be equal to the offset of the last element of an
> otherwise identical structure that replaces the flexible array member
> with an array of unspecified length", however gcc may add padding after
> the flexible array member. The numbers produced by the following program
> should all be equal.
> 
> $ 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
> $
> 
> Tony.
> -- 
> f.a.n.finch <dot@dotat.at> http://dotat.at/
> BAILEY: CYCLONIC 3 IN NORTH AT FIRST, OTHERWISE WESTERLY 4 OR 5, OCCASIONALLY
> 6. RAIN OR SQUALLY SHOWERS. MODERATE OR GOOD.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


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