Disappearing function call

andy at Shasta.UUCP andy at Shasta.UUCP
Tue Oct 28 18:11:23 AEST 1986


#define	DEBUG	1 /* or 0 as appropriate */

#define	debug	if( DEBUG ) fprintf

(Replace fprintf with the appropriate function.)

Then you can write:
    debug( stderr, "wrong args %d, %s, %p\n", d, s, p ); /* %p is bogus */

Either your compiler eliminates dead code or it doesn't.
If it doesn't, other dead code and missed opportunities to generate
good code will swamp the overhead of checking if 0 is still 0 and
skipping the fprintf.  If it does, the fprintf (and its args) will be
removed.

-andy

ps - Yes, I know my debug will pick up a following "else".  I use "extra"
{}'s and debug( <args> ) shouldn't be followed by an "else" anyway; I'll
document that feature and let it slide.  It is difficult to handle all
possible constraints with something as limited as cpp; "debug( (<args>) )"
grates on me.

I'd rather write "if( DEBUG ) <code>" directly than "debug( <args> )"
because it doesn't restrict <code>.
-- 
Andy Freeman
UUCP:  ...!decwrl!shasta!andy forwards to
ARPA:  andy at sushi.stanford.edu
(415) 329-1718/723-3088 home/cubicle



More information about the Comp.lang.c mailing list