Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.supernews.com!news.supernews.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 21 Oct 2012 18:09:12 -0500 From: Steve Thompson Newsgroups: comp.lang.c Subject: Re: Compatibility question Date: Sun, 21 Oct 2012 22:54:51 +0000 Organization: Friends of the Galactic Collective Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit User-Agent: NN 6.4.3 Lines: 62 X-Trace: sv3-D14JD3zEdVrCsyvhCMwvQ5xVAz0oJ5qCyR4IN7zB12hWdB3ZigJ1/sNMMb9V0qyovOPeV81aUP5Jex9!PYK2SiXcY8KIPc84f+UBt6Rd9y+7aLTtkQ20tshUNQGT6jnBmNstvvim7IzlPA== X-Complaints-To: www.supernews.com/docs/abuse.html X-DMCA-Complaints-To: www.supernews.com/docs/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3600 Xref: csiph.com comp.lang.c:27477 On Sun, Oct 21, 2012 at 09:26:54AM +1300, Ian Collins wrote: > On 10/20/12 17:01, Steve Thompson wrote: > >On Sat, Oct 20, 2012 at 04:20:53PM +1300, Ian Collins wrote: > >>On 10/20/12 16:08, Steve Thompson wrote: > >>>I have been using structures containing zero-length arrays as a means > >>>of combining descriptors and array data in a single allocation unit. > >>>Something like the following is a common idiom: > >>> > >>>struct foo { > >>> int a, b; > >>> size_t len; > >>> char p[0]; > >>>}; > >> > >>That should really be > >> > >> char p[]; > >> > >>Which is the standardised (as of C99) form of the struct hack. > >> > >>>So far, so good. My questions regards the availability of zero-length > >>>arrays in common systems. That is, if I wish my code to be compatible > >>>with most C compilers in use today, should I worry about avoiding > >>>zero-length arrays, or is the feature old enough, with sufficient > >>>prevalence to make it's use reasonable in portable code? > >> > >>Most compilers should support the standard form, or the zero length > >>array form of the struct hack. > > > >I was mainly wondering what the cutoff date was for this feature. If > >C compilers deployed within the last ten years or so support the p[0] > >idiom, I won't feel as if I'm excluding platforms that might otherwise > >be expected to run my code. The workaround is not a huge problem > >since the accesses will be insulated from the user by my functions, > >but it is less trouble to write if I don't have to cast everything. > > As Ben pointed out, the pre-C99 form was a single element array. I > think the zero element array is a gcc extension. So if you really want > to support every possible compiler, use p[1] and document what you are > doing. Otherwise use the standard form. Working on another part of the software I'm writing, I learned today that I must use TLS. There is no point trying to get around that constraint, so the list of potential targets is going to shrink quite a lot. Now that I've noticed that the GCC documentation has been updated (I may not have read that section for ten years, now that I think of it), it does indeed say that the C99 standard includes support for flexible arrays, with similar semantics to the old gcc zero subscript hack, so I will use that. Regards, Steve Thompson -- GOTO's are like guns. Safety is entirely contingent on the user.