Google Groups

Why is this legal?


John F. Woods Feb 25, 1992 3:25 PM
Posted in group: comp.std.c
bhou...@hopi.intel.com (Blair P. Houghton) writes:
>In article <10...@ksr.com> j...@ksr.com (John F. Woods) writes:
>>bhou...@hopi.intel.com (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.  3.3.3.4, 1.6, six two and
>>even, over and out.
>But tell us WHY, Dr. Define....

"3.1.2.5 Types
 A structure or union type of unknown content (as described in 3.5.2.3)
 is an incomplete type."
"3.3.3.4 The sizeof Operator
 Constraints
    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.