🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #6140

Re: "expandable" structs with last element declared using [1]

#6140
From: Karl Heuer
Date: Thu, 21 Dec 1989 22:00
41 lines
2055 bytes
Here's a challenge to those who believe that expandable structs are not legal
in a strictly conforming program.  I have enclosed six code fragments.  If you
agree that [0] is clearly legal (it doesn't even use the struct-pointer
|foo|), but maintain that [5] is not, then there must be some value n for
which you believe that fragment [n-1] is legal but fragment [n] is not.  I
would be interested in hearing where you would draw the line, and your reasons
for believing that the legality changes at that point.

Assume global declarations
	typedef struct foo_struct { int bar; char baz[1]; } T;
	T *foo;  void *vp;  char *cp;

[0]	vp = malloc(sizeof(T)+1);
	foo = (T *)vp;
	cp = (char *)vp;
	cp[offsetof(T, baz[0]) + 1] = '\0';

[1]	foo = (T *)malloc(sizeof(T)+1);
	cp = (char *)foo;
	cp[offsetof(T, baz[0]) + 1] = '\0';

[2]	foo = (T *)malloc(sizeof(T)+1);
	cp = (char *)foo + offsetof(T, baz[0]) + 1;
	*cp = '\0';

[3]	foo = (T *)malloc(sizeof(T)+1);
	cp = (char *)(&foo->baz[0]) + 1;
	*cp = '\0';

[4]	foo = (T *)malloc(sizeof(T)+1);
	cp = &foo->baz[1];
	*cp = '\0';

[5]	foo = (T *)malloc(sizeof(T)+1);
	foo->baz[1] = '\0';

(Please don't bother to reply if you haven't read at least one draft of the
Standard.  The question is not whether it's useful, nor whether current
compilers do or do not accept it, but whether the pANS permits it.)

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
Technical Details
Message-ID: <15509@haddock.ima.isc.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!think!husc6!spdcc!ima!haddock!karl
References: <UZTerlu00VcJEDulRd@andrew.cmu.edu> <463@cpsolv.UUCP> <477@longway.TIC.COM> <468@bbxsda.UUCP> <4379@cuuxb.ATT.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!think!husc6!spdcc!ima!haddock!karl
From: karl@haddock.ima.isc.com (Karl Heuer)
Newsgroups: comp.std.c
Subject: Re: "expandable" structs with last element declared using [1]
Message-ID: <15509@haddock.ima.isc.com>
Date: 21 Dec 89 22:00:00 GMT
References: <UZTerlu00VcJEDulRd@andrew.cmu.edu> <463@cpsolv.UUCP> <477@longway.TIC.COM> <468@bbxsda.UUCP> <4379@cuuxb.ATT.COM>
Reply-To: karl@haddock.ima.isc.com (Karl Heuer)
Distribution: comp.std.c
Organization: Interactive Systems, Cambridge, MA 02138-5302
Lines: 41