sizeof a struc field

Doug Gwyn gwyn at smoke.BRL.MIL
Sun Oct 22 09:09:38 AEST 1989


In article <7685 at cdis-1.uucp> tanner at cdis-1.uucp (Dr. T. Andrews) writes:
[I (Gwyn) said previously:]
>) I said that dereferencing a null pointer CONSTANT was meaningless,

Please IGNORE the rest of that, because it is a side issue having
NOTHING to do with the formal logical argument against
>	sizeof(((type *)0)->member)		/* cited construct */

I was responding there to your claim that the compiler shouldn't
flag such misusage at compiler time, as a general principle, only
loosely associated with this particular example.

>So far, we have heard that dereferencing a null pointer is garbage
>(but we aren't dereferencing one), and that null pointer CONSTANTS
>shouldn't be dereferenced (we still aren't dereferencing one),

If you take my discussion in its original context, it is clear
that I'm using "dereference" as colloquial shorthand for "apply
the -> operator to".

>and that an explicitly cast null pointer lacks type information (a claim
>which is hard to swallow).

You invented this last claim yourself, because I certainly never said it.
I did say that the construct under discussion as the operand of sizeof
has no valid type.  In fact it has no validity, period.

>What we have not heard is a convincing explanation of why the cited
>construct should be considered harmful.  No excuse for an
>implementation that might blow up on it has been given.  Further,
>no useful alternative has (to date) shown up.

I'm not obliged to address these points, although they were in fact
given careful consideration by X3J11 while the Standard was being
drafted.  My argument is that the Standard deliberately does not
assign a meaning for
	((type *)0)->member
This is a consequence of the semantics: "A postfix expression followed
by an arrow -> and an identifier designates a member of a structure or
union object".  But there is no object in this case (a null pointer
does not point to an object).  Thus, even though the type constraints
of the left operand were obeyed, this usage is a semantic violation.
Being a meaningless construct, its type is of course also meaningless
-- even though you think you know what it "should" be.  And, finally,
sizeof(meaningless) is still meaningless.

If you want to argue that the Standard should have assigned a meaning
in this case, you should have sent in a comment to that effect during
the public review process.  However, I doubt very much that you could
have made a sufficiently persuasive case for it.



More information about the Comp.lang.c mailing list