Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Empty statement

Reply
Thread Tools

Empty statement

 
 
Maksim Sipos
Guest
Posts: n/a
 
      02-20-2004
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?

--
Maksim Sipos


 
Reply With Quote
 
 
 
 
gabriel
Guest
Posts: n/a
 
      02-20-2004
What about:

#define DEBUG(x) ;

Does this work?

--
gabriel
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      02-20-2004
Maksim Sipos wrote:
>
> 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." |
+---------+----------------------------------+-----------------------------+

 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      02-20-2004
Maksim Sipos wrote:

> 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

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
 
Reply With Quote
 
Maksim Sipos
Guest
Posts: n/a
 
      02-20-2004
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

"David Rubin" <(E-Mail Removed)> wrote in message
news:c15bbl$(E-Mail Removed)...
> Maksim Sipos wrote:
>
> > 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
>
> --
> Andre, a simple peasant, had only one thing on his mind as he crept
> along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
> -- unknown



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-20-2004
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) ;

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      02-20-2004
"Maksim Sipos" <(E-Mail Removed)> wrote in message
news:2BrZb.32323$(E-Mail Removed)...
> 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!


 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      02-20-2004
On Fri, 20 Feb 2004 17:37:02 GMT, "Maksim Sipos"
<(E-Mail Removed)> wrote:

>
>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.

--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
Peter Pichler
Guest
Posts: n/a
 
      02-20-2004
"gabriel" <(E-Mail Removed)> wrote in message
news:d2c9a$40362229$d01d981e$(E-Mail Removed) rvers.com...
> 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


 
Reply With Quote
 
Guillaume
Guest
Posts: n/a
 
      02-20-2004
> 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".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DetailsView ASP Control and an empty field not an empty record. ButlerDJIAM ASP .Net 0 11-09-2006 06:40 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
empty lists vs empty generators Brian Roberts Python 12 05-04-2005 08:59 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM
empty/non-empty element John XML 1 07-16-2003 10:23 AM



Advertisments