Disappearing function call

Frank Whaley few at well.UUCP
Fri Oct 3 03:21:46 AEST 1986


In article <357 at cullvax.UUCP> drw at cullvax.UUCP (Dale Worley) writes:
>What I want to do is to write a function call of a variable number of
>arguments:
>	debug(x, y, z, ...)
>that will generate no code when the symbol DEBUG is not defined, but
>generate a call of some function (say, debug_()) when it is.

Numerous ugly examples deleted.

I had to do this just recently, and took advantage of the fact that my
compilers "optimize out" code like this:

	if (0) function();

So assuming:

	#ifdef	DEBUG
	#define	_DEBUG	1
	extern void _debug(char *,)	/*  debugging printf()  */
	#else
	#define	_DEBUG	0
	#endif
	#define	debug	if(_DEBUG)_debug

a statment like:

	debug("%d:%d:%d\n", a, b, c);

becomes either:

	if(1)_debug("%d:%d:%d\n", a, b, c);
or
	if(0)_debug("%d:%d:%d\n", a, b, c);	/*  no code generated  */

All of my compilers generate the strings anyway, so I used a set of external
strings (bracketed by #ifdef's) for all debugging messages.  Some compilers
require an optimizer pass to eat the dead code.

-- 
Frank Whaley
Senior Engineer, Beyond Words
UUCP:		hplabs!
	   ihnp4!ptsfa!
	seismo!lll-crg!well!few
ARPA:	well!few at lll-crg.ARPA

Water separates the people of the world;
wine unites them.



More information about the Comp.lang.c mailing list