sizeof a struc field

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Oct 28 04:10:31 AEST 1989


In article <4700046 at m.cs.uiuc.edu> kenny at m.cs.uiuc.edu writes:
>#define NEW(p) ((p) = malloc_and_check ((size_t) (sizeof *(p))))
>[appears to be claimed to be] illegal under the proposed
>standard, because it dereferences the uninitialized pointer p.

No, there is nothing wrong with that; the argument of sizeof is
not evaluated, and so a reference via the uninitialized pointer
cannot occur.

Since I'm the one claiming that sizeof(((foo*)0)->member) is invalid,
let me try ONE MORE TIME to clarify what I think makes it invalid.
It is invalid because the operand of sizeof is invalid, and the
operand of sizeof is invalid because you are not allowed to
write ((foo*)0)->member EVEN THOUGH the null pointer does have
type "pointer to foo" (and we are assuming that foo objects do have
"member" member designators).  I believe that a compiler is allowed
to diagnose that construct as an abuse of a null pointer constant.
There may be Standard-wise people who disagree with me on this,
but I haven't been getting much valid counterargument from them.
There HAS been a lot of argument against points that I did not
think I was trying to make!



More information about the Comp.lang.c mailing list