variable-length struct hack
Frans Meulenbroeks
meulenbr at cstw68.prl.philips.nl
Sat Dec 9 00:41:32 AEST 1989
In article <15364 at haddock.ima.isc.com> karl at haddock.ima.isc.com (Karl Heuer) writes:
[about dirent.h & the form of a dirent struct]
>I question this. It seems to me that
> typedef struct { junk_t xx; char name[1]; } T;
> T *p = (T *)malloc(sizeof(T) + strlen(s));
> strcpy(p->name, s);
>is legal and portable, and I believe I can rigorously prove it from the rule
>"objects are composed of bytes":
[proof deleted; see original article]
Karl, I like your proof. However, a struct definition like that
does not allow indexing in the name array under implementations which
check for array out of bounds. (by the way, does ANSI allow index out of
bound checks? Are they forbidden? Is it left to the implementor? I could
not find anything in the draft)
E.g.: if s = "Hello World", I'd like p->name[3] to result in an 'l', and
not in a syntax error.
I for me, I would prefer char *name instead of char name[1].
Frans Meulenbroeks (meulenbr at cst.prl.philips.nl)
Centre for Software Technology
( or try: ...!mcvax!phigate!prle!cst!meulenbr)
More information about the Comp.std.c
mailing list