Re: Zero length array declaration

From: Flash Gordon (spam_at_flash-gordon.me.uk)
Date: 12/02/04


Date: Thu, 2 Dec 2004 13:54:07 +0000

On Thu, 02 Dec 2004 02:38:56 GMT
"Mabden" <mabden@sbc_global.net> wrote:

> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:lnpt1tbh9v.fsf@nuthaus.mib.org...
> > "Method Man" <a@b.c> writes:
> > > "RS" <rs.naukri@gmail.com> wrote in message
> > > news:7dff197e.0412011149.27f66041@posting.google.com...
> > >> Hi,
> > >>
> > >> Looking to see if the following construct is valid:
> > >> typedef struct {
> > >> int foo;
> > >> char bar[0];
> > >> } foobar;
> > >>
> > >> Basically, the idea is to have the structure above point to a
> message
> > >> buffer that has a 4-byte integer followed by a stream of variable
> > >> number of bytes. The structure member "bar" is used to reference
> the
> > >> stream of bytes. The above code compiles fine with a GNU based
> > >PPC> cross-compiler running on Solaris and seems to do the function
> > >> intended.
>
> If you want "to have the structure above point to a message buffer",
> you should use a pointer to a buffer containing a message. This buffer
> could be an array of strings, for instance.

He does *not* want the structure to point to a buffer containing the
message. The int foo is almost certainly part of the message comming
over a communications link followed by a number of bytes. For instance,
the bytes recieved might be
   int 5
   byte 1
   byte 2
   byte 3
   byte 4

To do what you suggest the routine receiving this would first have to
allocate memory for the "header" integer and a pointer, allocate another
block for the rest of the message, set up the pointer and also change
where it is sending the data to etc. Also, the entire lot might be
comming in as one block of data so your solution could also meen copying
the data around even more.

> > >> My question is: Is it legal to declare an array with zero length?
> Or
> > >> should bar have been declared to be at least one element in
> > >length?>
> > >
> > > Strictly speaking, accessing an array beyond its bounds results in
> undefined
> > > behaviour according to the Standard.
> >
> > Strictly speaking, declaring a 0-sized array is illegal. Some
> > compilers may let you get away with it. Compilers that disallow
> > 0-sized arrays may let you implement the struct hack with
> > char bar[1];
> > The declaration is legal, but accessing elements beyond bar[0]
> > invokes undefined behavior (which, if you've allocated enough
> > memory, is likely to work aryway).
>
> Especially if "aryway" means "not very well". I don't understand why
> you would even pursue such a line of reasoning when all the OP wants
> (read: needs) is to use a pointer. His post indicated what he wanted,
> so why go off on unsupported tangents that imply acceptance for arrays
> of zero length or whatever. It's a silly thing to run a thread on.

No, Keith knows EXACTLY what he is tlking about and the OP was taking
exactly the right approach. It is just unfortunate that the C89 standard
did not bless a way of doing it and that the C99 standard did not
include support for [0] and/or the [1] struct hack which had the benefit
of existing practice.

Also, although not covered by the ISO standard it is a very commonly
supported extension. I would even go as far as to reject a compiler on
the basis of it not supporting this and instead select one where it was
supported, where it not for the fact I haven't come across a compiler
where it fails.

-- 
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


Relevant Pages

  • Re: reading a text file into a string
    ... it should be supported by all compilers. ... compilers that do support such alignments, ... case of a String buffer when reading files is a perfect case in point. ... cache line, then that line will stay resident in L1 cache. ...
    (comp.lang.ada)
  • Re: Zero length array declaration
    ... If you want "to have the structure above point to a message buffer", ... should use a pointer to a buffer containing a message. ... be an array of strings, ... > compilers may let you get away with it. ...
    (comp.lang.c)
  • =?WINDOWS-1252?Q?2.6.23-rc3-mm1_--_drivers/net/wireless/rt2x00mac.c:65:_e?= =?WI
    ... 'ieee80211_ctstoself_get' makes pointer from integer without a cast ... # CPU idle PM support ... # Infrared-port device drivers ...
    (Linux-Kernel)
  • [net-next PATCH 4/5] igb: Add support for enabling VFs to PF driver.
    ... pointer to the hardware struct ... under the terms and conditions of the GNU General Public License, ... * @msg: The message buffer ... * returns SUCCESS if it successfully copied message into the buffer ...
    (Linux-Kernel)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)