sizeof in #if?
Jim Blandy
blandy at marduk.cs.cornell.edu
Fri Jul 29 01:40:36 AEST 1988
In article <4543 at rpp386.UUCP> jfh at rpp386.UUCP (The Beach Bum) writes:
>sizeof has nothing to operate on. there is no means of specifying the
>datatype or variable whose size is to be determined.
Absolutely right. Expressions in #ifs are evaluated by the C
preprocessor (obviously), which doesn't know anything about variables
or their types. Remember that cpp is often implemented as a separate
program, with no access to the information the compiler maintains about
a variable's type, etc. In order to get it to do sizeof, cpp would
have to parse declarations out of the source code it was preprocessing
and store them in its own symbol table - a big duplication of effort,
and a disaster for other programs that use CPP on non-C text (like
Cornell's Synthesizer Generator).
An alternative to consider: it's a simple task for a compiler to
recognize that the condition for an if ( ) statement is a constant,
use this knowledge in guessing which statements are unreachable, and
drop unreachable code. For example, I wouldn't be surprised if a
good compiler would take
if (sizeof(a_structure) > THELARGESTIWANTTOTHINKABOUT)
... action for big structures ...
else
... action for small structures ...
, recognize that the condition is constant, and code only the appropriate
branch. This may provide the capability you want.
--
Jim Blandy - blandy at crnlcs.bitnet
"And now," cried Max, "let the wild rumpus start!"
More information about the Comp.std.c
mailing list