Re: Zero length array declaration
From: Flash Gordon (spam_at_flash-gordon.me.uk)
Date: 12/02/04
- Next message: Tim Prince: "Re: floating point over-/under-flow"
- Previous message: Al Bowers: "Re: Clunky C cleanup code"
- In reply to: Mabden: "Re: Zero length array declaration"
- Next in thread: jacob navia: "Re: Zero length array declaration"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Tim Prince: "Re: floating point over-/under-flow"
- Previous message: Al Bowers: "Re: Clunky C cleanup code"
- In reply to: Mabden: "Re: Zero length array declaration"
- Next in thread: jacob navia: "Re: Zero length array declaration"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|