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

Karl Heuer karl at haddock.ima.isc.com
Fri Dec 22 04:12:25 AEST 1989


In article <468 at bbxsda.UUCP> scott at bbxsda.UUCP (Scott Amspoker) writes:
>...I see no way you would get screwed unless sizeof(char) is more than 1...
>Maybe someone thinks that structure fields need not be allocated in any
>particular order.

In ANSI C, sizeof(char) must be 1 and structure members are allocated in the
order that they're declared.  But even among people who realize this, there is
disagreement as to the legality of the construct in question.

In article <4171 at scolex.sco.COM> seanf at sco.COM (Sean Fagan) writes:
>[Symbolic debugging information...]  Now, imagine an architecture that does
>that in hardware (well, microcode, probably).  Guess what, there *are*
>machines that do!  ...  Turning on bounds-checking in your compiler, if it
>supported it, would cause it to fail.

This doesn't prove that the construct is illegal.  An equally valid
interpretation is that implementations that like to do bounds-checking must
settle for a weaker check in these cases, if they claim to be ANSI-conforming.

In article <4379 at cuuxb.ATT.COM> mmengel at cuuxb.UUCP (Marc W. Mengel) writes:
>Gee guys, you *told* the compiler that foo->baz only has one character in it;
>of *course* it's wrong to reference foo->baz[1].

Intuition is not necessarily a good guide to understanding the pANS.

>Sooner or later you will find a machine with really wierd base/offset
>limitations, and a compiler will try to generate
>	mov.b $1,8(%a2)
>when the offset "n" on a "n(%am)" can only be 0..7, and you'll be sorry.  The
>compiler will have been correct in it's code generation because it "knew"
>that the subscript had to be zero.

That's a valid argument for why it *should* be illegal, but again, it doesn't
make it so.  If the construct *is* legal, then a compiler such as you describe
is not ANSI-conforming, and hence is of little interest in this discussion.

(Note: in this article I have responded to the arguments of the three previous
posters, but have not supplied any evidence that would actually help answer
the question.  I'll do that in a separate posting.)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.std.c mailing list