473,783 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Empty statement

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
Nov 14 '05 #1
10 4295
What about:

#define DEBUG(x) ;

Does this work?

--
gabriel
Nov 14 '05 #2
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." |
+---------+----------------------------------+-----------------------------+

Nov 14 '05 #3
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
Nov 14 '05 #4
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" <no****@nowhere .net> wrote in message
news:c1******** @netnews.proxy. lucent.com...
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

Nov 14 '05 #5
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) ;

--
Er*********@sun .com
Nov 14 '05 #6
"Maksim Sipos" <ma*@sipos-software.com> wrote in message
news:2B******** ***********@twi ster.nyroc.rr.c om...
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!
Nov 14 '05 #7
On Fri, 20 Feb 2004 17:37:02 GMT, "Maksim Sipos"
<ma*@sipos-software.com> 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
re************* ***********@att .net
Nov 14 '05 #8
"gabriel" <no@no--spam.com> wrote in message
news:d2******** *************** ***@msgid.megan ewsservers.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_o ther)
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
Nov 14 '05 #9
> 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".
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
11879
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at the top of my script I assign a few variables to incoming GET and POST values. $login = clean($_POST, 30); $passwd = clean($_POST, 30);
3
1670
by: Emmanuel Thomé | last post by:
This is a comment aside the empty class behavior FAQ. I understand there are a fair number of reasons which make empty classes have non-zero size (except as base classes). If ``class foo'' were of size 0, then: - an object of type foo may lie at the same address as another. - arrays of foo objects would have size 0, and pointer arithmetic on array cell would break. - requirements of the standard explicitly stating objects have
3
2000
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the invoked member function. It is possible to move the position into the object by adding a data...
11
4608
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
3
3022
by: Joachim | last post by:
Hi I am a beginner in VB.NET, and have a problem with empty field in Access I have transfered a worksheet in Excel to Access table. Some of the cels are empty I use VB.NET program to acces this Access table (product.mdb) One of the statements is to validate whether the field empty or not My statement is If Trim(dr_Product("bt_m3event")) = "" Or Trim(dr_Product("bt_m3event")) = " " The
59
8838
by: Steve R. Hastings | last post by:
So, Python 2.5 will have new any() and all() functions. http://www.python.org/dev/peps/pep-0356/ any(seq) returns True if any value in seq evaluates true, False otherwise. all(seq) returns True if all values in seq evaluate true, False otherwise. I have a question: what should these functions return when seq is an empty list?
13
2864
by: lithoman | last post by:
I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the reader.Read(), it has a row with 13 data columns, but they're all empty. So, my GetInt() function throws an error. When it jumps to the catch(), reader's columns then show the proper data. What gives? I have reader._data (which is the ID...
2
2049
by: Shum | last post by:
Hi! i have a question.. I'm filling a dataset from a table in which some rows are empty, i dont want those empty records to be filled in the dataset.. Does any one know how to restrict it to only non empty records.. ??
2
2211
by: talkaboutquality | last post by:
Need to define a macro as, say, #ifdef NEED_FUNCTION foo(x) #else #endif
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.