This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: c/9058: structure with flexible array member: offsetof() !=sizeof()
- From: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>
- To: Manfred Spraul <manfred at colorfullife dot com>
- Cc: <gcc-gnats at gcc dot gnu dot org>, <gcc-bugs at gcc dot gnu dot org>
- Date: Thu, 26 Dec 2002 22:24:11 +0000 (GMT)
- Subject: 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