From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11734 invoked by alias); 26 Dec 2002 22:26:02 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 11683 invoked by uid 71); 26 Dec 2002 22:26:02 -0000 Date: Thu, 26 Dec 2002 14:26:00 -0000 Message-ID: <20021226222602.11680.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: "Joseph S. Myers" Subject: Re: c/9058: structure with flexible array member: offsetof() != sizeof() Reply-To: "Joseph S. Myers" X-SW-Source: 2002-12/txt/msg01303.txt.bz2 List-Id: The following reply was made to PR c/9058; it has been noted by GNATS. From: "Joseph S. Myers" To: Manfred Spraul Cc: , 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