Empty statement

M

Maksim Sipos

Hello,
I would like to have something like

#ifdef VERSION_DEBUG
#define DEBUG(x) DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x) <empty-statement>
#end

but I'm not sure how to implement the empty statement.. the problem, of
course is in the extra semicolon when the macro is called as in,
DEBUG(x); in release version. One of the possible solutions is to set

#define DEBUG(x) do{}while(0)

but I am not sure whether the compiler is smart enough
to optimize this. Any other ideas, comments?
 
K

Kenneth Brody

Maksim said:
Hello,
I would like to have something like

#ifdef VERSION_DEBUG
#define DEBUG(x) DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x) <empty-statement>
#end

but I'm not sure how to implement the empty statement.. the problem, of
course is in the extra semicolon when the macro is called as in,
DEBUG(x); in release version.
[...]

Simply define it as nothing:

#define DEBUG(x)

Why is "the extra semicolon" a problem in the release version?

Can you give a sample of code where it doesn't work?

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
 
D

David Rubin

Maksim said:
Hello,
I would like to have something like

#ifdef VERSION_DEBUG
#define DEBUG(x) DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x) <empty-statement>
#end

All that is required is

#define DEBUG(x) /* nothing here */
but I'm not sure how to implement the empty statement.. the problem, of
course is in the extra semicolon when the macro is called as in,
DEBUG(x); in release version.

The semicolon will not cause any problems since it is itself a valid "empty"
statement. For example

int foo(void)
{
int i; ; ; ;;;;
;
;;;;
for(i=0; i<10; ++i);
if(i > 10){
;
}
return i;
}

is perfectly legal. Now, imagine that each semicolon above which does not
terminate a statement is proceeded by DEBUG(whatever).
One of the possible solutions is to set

#define DEBUG(x) do{}while(0)

but I am not sure whether the compiler is smart enough
to optimize this. Any other ideas, comments?

This statement should not be optimized away since the intent is to execute the
do-while *at least* once. This is actually a very good way to implement
debugging macros since it protects against the dangling else problem as well as
introduces a scope for debugging-specific variables. For example,

if(pred)
DEBUG(stuff);
else
return 0;

If you've defined

#define DEBUG(x) if(dbgEnabled) fprintf(stderr, "DBG: x=%d\n", x)

you'll run into problems.

/david
 
M

Maksim Sipos

You, as well as Kenneth Brody and Gabriel are absolutely right.
For some reason I thought it was impossible to have hanging
semicolons. The next question is, assume I have the following code:

if (i == 0) DEBUG(1) ;

In the release version (DEBUG is an empty statement), will the
compiler create the "if i equals 0 then do nothing" or will it optimize
it out?
Thank you,
Maksim Sipos
 
E

Eric Sosman

Maksim Sipos wrote: [top-posted; he'll learn better, we hope]
You, as well as Kenneth Brody and Gabriel are absolutely right.
For some reason I thought it was impossible to have hanging
semicolons. The next question is, assume I have the following code:

if (i == 0) DEBUG(1) ;

In the release version (DEBUG is an empty statement), will the
compiler create the "if i equals 0 then do nothing" or will it optimize
it out?

"Yes."

That is, it's up to the compiler, and a conforming
program can't tell whether the test was performed or not.

There are some situations where the test must *not*
be removed by optimization. Here are a few:

if (i == 0) ;
else puts ("Eeek!");


#define i putchar('\n')
if (i == 0) ;


volatile int i;
...
if (i == 0) ;
 
X

xarax

Maksim Sipos said:
You, as well as Kenneth Brody and Gabriel are absolutely right.
For some reason I thought it was impossible to have hanging
semicolons. The next question is, assume I have the following code:

if (i == 0) DEBUG(1) ;

In the release version (DEBUG is an empty statement), will the
compiler create the "if i equals 0 then do nothing" or will it optimize
it out?
Thank you,
Maksim Sipos
/snip/

Well, optimizations are off-topic here, but you can probably
assume that a reasonably smart compiler will generate very
few, if any, instructions for "if (i == 0) ;".

--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!
 
A

Alan Balmer

if (i == 0) DEBUG(1) ;

In the release version (DEBUG is an empty statement), will the
compiler create the "if i equals 0 then do nothing" or will it optimize
it out?

Ask the compiler ;-) Or ask on a newsgroup specific to the
implementation you're using. Unlike your first question, this one is
off-topic, since the C standard has nothing to say on the subject.
 
P

Peter Pichler

gabriel said:
What about:

#define DEBUG(x) ;

[ In the following context:

#ifdef VERSION_DEBUG
#define DEBUG(x) DebugFnc(x)
#end

#ifdef VERSION_RELEASE
#define DEBUG(x) <empty-statement>
#end
]
Does this work?

Of course not. Try:

if (something_or_other)
DEBUG(x);
else
DEBUG(y);

Your solution translates to two semicolons, which in this case is a syntax
error.

To the OP: try something like:

#define DEBUG(x) ((void)0)

Neat, portable, syntactically safe and (generally) does not eat CPU cycles.

Peter
 
G

Guillaume

if (i == 0) DEBUG(1) ;
In the release version (DEBUG is an empty statement), will the
compiler create the "if i equals 0 then do nothing" or will it optimize
it out?

The question is: will it perform whatever is in the 'if' condition or
not?

If the compiler is half-decent, it will of course generate no code for
this, provided that the condition doesn't have any side effect. If it
does, the compiler *has* to generate the condition inside the 'if', but
it will not generate any "flow control" code (since there is no two
different paths).

In your example, if 'i' is a variable, the whole 'if' statement will
lead to no code at all. Now, if 'i' is actually a macro that could have
side effects, the compiler has to generate the code for this macro.

For instance:

if (printf("Hello.\n")) ;

The 'printf' call can't be "optimized out".
 
D

Derk Gwen

# Hello,
# I would like to have something like
#
# #ifdef VERSION_DEBUG
# #define DEBUG(x) DebugFnc(x)
# #end
#
# #ifdef VERSION_RELEASE
# #define DEBUG(x) <empty-statement>

If you're not satisfied with
#define DEBUG(x)
then you can use
#define DEBUG(x) 0
or
#define DEBUG(x) ((void)0)
if you have whiny compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top