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

Joseph S. Myers jsm28@cam.ac.uk
Thu Dec 26 14:26:00 GMT 2002


The following reply was made to PR c/9058; it has been noted by GNATS.

From: "Joseph S. Myers" <jsm28@cam.ac.uk>
To: Manfred Spraul <manfred@colorfullife.com>
Cc: <gcc-gnats@gcc.gnu.org>,  <gcc-bugs@gcc.gnu.org>
Subject: Re: c/9058: structure with flexible array member: offsetof() !=
 sizeof()
Date: Thu, 26 Dec 2002 22:24:11 +0000 (GMT)

 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
 



More information about the Gcc-prs mailing list