🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #5245

Re: sizeof in #if?

#5245
From: Jim Blandy
Date: Thu, 28 Jul 1988 15:40
30 lines
1974 bytes
In article <4543@rpp386.UUCP> jfh@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@crnlcs.bitnet
"And now," cried Max, "let the wild rumpus start!"
Technical Details
Message-ID: <19730@cornell.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!attcan!uunet!husc6!mailrus!cornell!blandy
References: <705@bnr-rsc.UUCP> <11556@steinmetz.ge.com> <11637@steinmetz.ge.com> <11654@steinmetz.ge.com> <12685@mimsy.UUCP> <4543@rpp386.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!attcan!uunet!husc6!mailrus!cornell!blandy
From: blandy@marduk.cs.cornell.edu (Jim Blandy)
Newsgroups: comp.std.c
Subject: Re: sizeof in #if?
Message-ID: <19730@cornell.UUCP>
Date: 28 Jul 88 15:40:36 GMT
References: <705@bnr-rsc.UUCP> <11556@steinmetz.ge.com> <11637@steinmetz.ge.com> <11654@steinmetz.ge.com> <12685@mimsy.UUCP> <4543@rpp386.UUCP>
Sender: nobody@cornell.UUCP
Reply-To: blandy@cs.cornell.edu (Jim Blandy)
Organization: Cornell Univ. CS Dept, Ithaca NY
Lines: 30