sizeof a struc field

Dr. T. Andrews tanner at cdis-1.uucp
Tue Oct 3 00:33:59 AEST 1989


In article <11185 at smoke.BRL.MIL>, gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
> You're not allowed to feed garbage to sizeof().  ((type *)0)->anything
> is garbage.
For some looping construct with "struct blunge *p;", then, we have
the interesting cases:

	for ( p=0 ;; )  {			/* p == (type *)0 */
		/* ... */
		if  (some_cond1)
			sz = sizeof(p->member);		/* garbage? */
		if  (some_cond2)
			p = p_malloc(sizeof(*p));	/* garbage? */
		/* ... */
	}

Remember that sizeof() is a compile-time operation.  Do we expect
the compiler to do flow analysis to determine whether we are feeding
a valid pointer into a sizeof() expression?

I should be pleased to hear WHY (((type *)0)->anything) is disallowed
in the context of compile-time expressions.  Is there some existing
compiler in which it is disallowed (why?) or has X3J11, after some
careful consideration, noted that there is good reason to discourage
it?

Note that the bald assertion that it is "garbage" is a little hard
to swallow.
-- 
...!bikini.cis.ufl.edu!ki4pv!cdis-1!tanner  ...!bpa!cdin-1!cdis-1!tanner
or...  {allegra attctc gatech!uflorida}!ki4pv!cdis-1!tanner



More information about the Comp.lang.c mailing list