This is the mail archive of the gcc-bugs@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: c/9058: structure with flexible array member: offsetof() !=sizeof()


On Thu, 26 Dec 2002, Manfred Spraul wrote:

> 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]);

You mean offsetof(struct a, c) + nr_entries * sizeof(short).

> 
> valid and portable?
> 
>     sizeof(struct a)+nr_entries*sizeof(short);
> 
> would waste 2 bytes.

Those would be the bytes of padding (that might or might not nominally be
needed, depending on the size of the array).  I don't believe their
absence can cause problems unless you directly assign structures for which
less memory than the size of the original structure has been allocated (in
which case you'll lose the flexible array members anyway) but you'll need
to study the detailed wording of the standard (as proposed to be amended)  
to work out what is guaranteed portable.

-- 
Joseph S. Myers
jsm28@cam.ac.uk


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