Google Groups

Why is this legal?

John F. Woods Feb 25, 1992 11:51 AM
Posted in group: comp.std.c (Blair P. Houghton) writes:
>In article <> (John F. Woods) writes:
>> (Blair P. Houghton) writes:
>>>struct foo *foo;
>>>main(){ printf("*foo size: %d\n", sizeof(struct foo) );
>>At this point, the compiler is at liberty to generate a program which formats
>>your hard disk drive.  In fact, it probably should., 1.6, six two and
>>even, over and out.
>But tell us WHY, Dr. Define....

" Types
 A structure or union type of unknown content (as described in
 is an incomplete type."
" The sizeof Operator
    The sizeof operator shall not be applied to an expression that has
 function type or an incomplete type, to the parenthesized name of such
 a type, ..."
"1.6 Definitions of Terms
 In this standard, ... "shall not" is to be interpreted as a prohibition.
 * Undefined behavior -- behavior, upon use of a nonportable or erroneous
 program construct, ... for which the standard imposes no requirements.
 Permissible undefined behavior ranges from ignoring the situation completely
 with unpredictable results, to having demons fly out of your nose."

In short, you can't use sizeof() on a structure whose elements haven't been
defined, and if you do, demons may fly out of your nose.

OK, OK; so the Standard doesn't *ACTUALLY* mention demons or noses.  Not as
such, anyway.