Re: OT: Language War (Re: "C" Manual)
On Sun, 6 Jan 2002 16:48:31 -0500 (EST), William T Wilson <fluffy@snurgle.org> wrote:
> On Sat, 5 Jan 2002, Eric G.Miller wrote:
>
> > is one of the reasons pointers to char are so common. However, there
> > is a little trick that's guaranteed to always work:
> >
> > struct foo {
> > size_t length;
> > char str[1];
> > };
[snip]
> It doesn't look particularly guaranteed to me. You're really allocating
> *three* pieces of memory here - one for the struct foo, one for the
> 1-character array chr, and one for the rest of the string. Your example
> assumes that chr will be located in memory immediately after foo - which
> it probably will, but it might not. It could be anywhere, the language
> makes no guarantee. The compiler might even choose to put the 1-char
> array before foo, so you can't use it without overwriting your struct
As a follow up. I checked around. According to the C FAQ (1995) #2.6, this
behavior was determined not to be strictly conforming but was believed
to be portable to all known implementations. ISO C99 defines it as
a valid construct (6.5.2.1 #15-17). It also allows that the array
may be an incomplete type "char str[];". The sizeof() operator will always
treat the struct as if the array had been defined as having a single
element.
Anyway, thanks for making me check my facts. Sometimes we get ideas about
things that aren't necessarily true...
--
Eric G. Miller <egm2@jps.net>
Reply to: