473,883 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of do...while(0) in ASSERT macro

Can anyone help with a quick query...

I've seen the ASSERT macro defined as:
#define ASSERT(f) \
do { \
if (!(f) && assertFailedOnL ine (THIS_FILE, __LINE__)) \
FatalExit (0); \
} while (0) \
When I comple this in debug mode the compiler warns "conditiona l
expression is constant", because of the while (0). Why is the ASSERT
macro defined this way? The loop only runs once so why not get rid of
the do..while and use:
#define ASSERT(f) \
if (!(f) && assertFailedOnL ine (THIS_FILE, __LINE__)) \
FatalExit (0); \
This also gets rid of the compiler warning.

Is there any reason to use the first definition?

Martin
Nov 13 '05
36 14139
Christopher Benson-Manica wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:

[
#define ASSERT(f) \
if (!(f) && assertFailedOnL ine(__FILE__, __LINE__)) \
FatalExit (0); ] ...works just fine without the do/while(0) silliness.


Does that mean that the do/while(0) bit is merely an obfuscating
device?


No. It is a device to increase the robustness of the code. Without
the do/while wrapper the macro will work in certain circumstances, but
have silently incorrect behaviour if used as the body of a braceless
"if" statement (which has a matching "else"). In this particular case
the macro /can/ be written to have the correct behaviour without
do/while:

#define ASSERT(f) \
(void)((!f || assertFailedOnL ine(__FILE__, __LINE__)) && (FatalExit(0), 0))

but if the macro must expand to multiple /statements/ then the
do/while trick (or something similar) is needed for the macro to have
the correct behaviour.
Jeremy.
Nov 13 '05 #11
Jeremy Yallop wrote:
#define ASSERT(f) \
(void)((!f || assertFailedOnL ine(__FILE__, __LINE__)) && (FatalExit(0), 0))


`!(f)', not `!f'.

--
Hallvard
Nov 13 '05 #12
Hallvard B Furuseth wrote:
Jeremy Yallop wrote:
#define ASSERT(f) \
(void)((!f || assertFailedOnL ine(__FILE__, __LINE__)) && (FatalExit(0), 0))


`!(f)', not `!f'.


Thanks, good catch.

Jeremy.
Nov 13 '05 #13
Christian Bau wrote:
In article <bp**********@s parta.btinterne t.com>,
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Christopher Benson-Manica wrote:
> Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:
>
>> ...works just fine without the do/while(0) silliness.
>
> Does that mean that the do/while(0) bit is merely an obfuscating
> device?


If you write:

if(foo)
bar();

then no, it is not.

If you write:

if(foo)
{
bar();
}

then yes, it is.


If you think that removing the braces in
if(foo)
{
bar();
}


shouldn't cause any problems, then it isn't.


But if you aren't ever going to do that, because you'd rather gnaw off your
own leg than use a single-statement if, then it is.

Over to you, Christian. :-)

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #14
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Christian Bau wrote:
In article <bp**********@s parta.btinterne t.com>,
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Christopher Benson-Manica wrote:

> Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:
>

If you write:

if(foo)
{
bar();
}

then yes, it is.


If you think that removing the braces in
if(foo)
{
bar();
}


shouldn't cause any problems, then it isn't.

But if you aren't ever going to do that, because you'd rather gnaw off your
own leg than use a single-statement if, then it is.


But if the person maintaining your code is not in the same
state of mind?

Alex
Nov 13 '05 #15
Alex wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:
Christian Bau wrote:

In article <bp**********@s parta.btinterne t.com>,
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote:

Christopher Benson-Manica wrote:

> Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:
>

If you write:

if(foo)
{
bar();
}

then yes, it is.

If you think that removing the braces in

if(foo)
{
bar();
}

shouldn't cause any problems, then it isn't.

But if you aren't ever going to do that, because you'd rather gnaw off
your own leg than use a single-statement if, then it is.


But if the person maintaining your code is not in the same
state of mind?


Then I'd be beside myself. :-)

Seriously, it's a reasonable point, but in my defence[1] I generally do my
level best to get the coding standards at a client site changed to mandate
compound statements if they don't already do so. I've had some degree of
success with this. :-)

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #16
[snips]

On Thu, 20 Nov 2003 18:18:33 +0000, Richard Heathfield wrote:
Seriously, it's a reasonable point, but in my defence[1] I generally do my
level best to get the coding standards at a client site changed to mandate
compound statements if they don't already do so. I've had some degree of
success with this. :-)


As in:

if ( x )
{
y();
}

This is, IMO, bad. It clutters code with unnecessary crud. If it helps
because y might be some mucked-up macro that does weird things with block
levels or other sorts of nastiness, I'd tend to focus on the problem -
the macro - rather than this sort of approach.

Then again, I'd tend to try to disallow macros beyond the most trivial in
the first place - use a function, not a macro, if it needs to "do things".
Nov 13 '05 #17
Kelsey Bjarnason wrote:
[snips]

On Thu, 20 Nov 2003 18:18:33 +0000, Richard Heathfield wrote:
I generally do
my level best to get the coding standards at a client site changed to
mandate compound statements if they don't already do so. I've had some
degree of success with this. :-)
As in:

if ( x )
{
y();
}


Yes, precisely. Well, not quite as much whitespace. I'd prefer:

if(0 != x)
{
y();
}

This is, IMO, bad.
That doesn't surprise me, but I must disagree with you.
It clutters code with unnecessary crud.
That's a negative description. Here's a positive one to balance: it
emphasises the logical structure of the code.
If it helps
because y might be some mucked-up macro that does weird things with block
levels or other sorts of nastiness, I'd tend to focus on the problem -
the macro - rather than this sort of approach.
No, the macro isn't the problem. People are the problem. They /will/ keep
turning this:

if(w != x)
y();

into this:

if(w != x)
y();
z();

and then wondering why z() is called more often than they expected. (Utter
foolishness, I know.)

No, I'm perfectly happy with my bracing style, thanks - even if you disagree
with it.
Then again, I'd tend to try to disallow macros beyond the most trivial in
the first place - use a function, not a macro, if it needs to "do things".


There are certain rather obvious uses for macros. (Bit-twiddling is one.
Wrapping __FILE__ and __LINE__ is another.)
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #18
On Sun, 23 Nov 2003 06:42:58 +0000, Richard Heathfield wrote:
No, the macro isn't the problem. People are the problem. They /will/ keep
turning this:

if(w != x)
y();

into this:

if(w != x)
y();
z();

and then wondering why z() is called more often than they expected. (Utter
foolishness, I know.)

No, I'm perfectly happy with my bracing style, thanks - even if you disagree
with it.


I'm going to offer what may be an odd opinion. I don't follow your style
Richard; I like to use single statements without braces after if(). I have
never seen a single case in which a programmer actually had any problem
with this style. But I would support mandatory bracing in a coding
standards document because I think it makes the code easier to read when
more than one person is working on it.
Nov 13 '05 #19
Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Yes, precisely. Well, not quite as much whitespace. I'd prefer:

if(0 != x)
{
y();
}
<SNIP>
No, the macro isn't the problem. People are the problem. They /will/ keep
turning this:

if(w != x)
y();

into this:

if(w != x)
y();
z();

and then wondering why z() is called more often than they expected. (Utter
foolishness, I know.)

What is wrong with simply

if(x) y();

???

--

John Devereux
Nov 13 '05 #20

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

Similar topics

7
3184
by: Stephen Tyndall | last post by:
I know the preprocessor is evil, but I'd like to know what's going on in the following code. The problem is when the num variable is used in the ASSERT macro inside main(). When running the code, I get the following error from Visual C++.NET 2003: warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant
4
2051
by: Leo | last post by:
Hi @ all. I looked up the implementation of the assert macro of my compiler (MinGW), because I wanna write my own assert. I found this: #define assert(x) ((void)0) #define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))
3
7741
by: Tony Johansson | last post by:
Hello Experts! I'm reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don' work. The text in the book says "To offer the programmer the choice of toggling between using and not using asser(), this function is used together with a macro called NDEBUG; when this macro is defined, assert() does nothing. If NDEBUG is not defined and the actual parameter of
21
7294
by: Giuseppe | last post by:
is assert() for debug only or not? Is it possible that I have seen the use of assert() in the Borland c++ 32 compiler (so assert is not for debug only)?
5
3173
by: Alex Vinokur | last post by:
Here are two programs. --- foo1.c --- #include <assert.h> #define FOO 10 int main() { assert (15 < FOO); return 0; }
47
3132
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I can see that it would be poor style to use it for commonly encountered errors, but what about truly exceptional errors that would rarely if ever be encountered?
28
5750
by: lovecreatesbeauty | last post by:
Besides printing out for example " a.out: p113.c:8: main: Assertion `0' failed. Aborted " and a switch option NDEBUG, what other benefits does assert() provide in any scope of designing, debugging/coding and/or testing? Do you prefer the if statement of the language to the assert MACRO of the precompiler?
30
2209
by: Tomás Ó hÉilidhe | last post by:
In C89, do we have to pass an int as an argument to assert? I've got code at the moment that does an assertion on pointer, e.g.: assert(p); , but I'm wondering if I should change that to: assert(0 != p);
11
9230
by: Francois Grieu | last post by:
Hi, I'm using an assert()-like macro to test a constant expression at compile time #define ASSERT(condition) struct{char assert_failure;} The idea is that this macro cause a compilation error if a constant condition is not true (or if the condition is not constant), with some
0
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10863
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
10422
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
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
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...
0
6006
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
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
4229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.