473,749 Members | 2,665 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 #1
36 14121
mr*****@totalis e.co.uk (Martin) wrote:
# 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); \

Why not try something like
if (p)
ASSERT(q)
else
s;
and see what happens.

You can prevent this by bracing
#define ASSERT(f) {\
if (!(f) && assertFailedOnL ine (THIS_FILE, __LINE__)) \
FatalExit (0); }
but now you have to remember that although ASSERT(q) looks like a statement,
if you put a ';' after it
if (p)
ASSERT(q);
else
s;
it breaks unexpectedly.

Now try the original definition of ASSERT in the above.

# This also gets rid of the compiler warning.

So does the right -w option.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
God's a skeeball fanatic.
Nov 13 '05 #2

"Martin" <mr*****@totali se.co.uk> a écrit dans le message de news:
cb************* *************@p osting.google.c om...
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?


I don't know how it's call in english, so I try to explain it.
It because "effets de bord" tha are problem due to what we can find just
before an after the use of this macro.
In some cases, one part of the macro is included in something we don't want
to.

example:
# define ADD(A, B) A + B

ADD(1, 2) * 3 get after preprocessing: 1 + 2 * 3
or I'm sure you want: (1 + 2) * 3

so, we prefer to use:
#define ADD(A, B) ((A) + (B))

When you use function, you can use the do { blablabla } while (0) that is
the same thing, addapted to function.
I hope you'll understand however my bad english.
Nov 13 '05 #3
On 15 Nov 2003 03:34:20 -0800
mr*****@totalis e.co.uk (Martin) wrote:
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?


if (x)
for (y=x; y>0 && a[x]!=TARGET]; y--)
ASSERT(a[x]==BAD);
else
y=-1;

Given your definition (minus spurious \) this expands to
if (x)
for (y=x; y>0 && a[x]!=TARGET]; y--)
if (!(a[x]==BAD) && assertFailedOnL ine (THIS_FILE, __LINE__))
FatalExit (0);;
else
y=-1;

Note the two semicolons after FatalExis(0) which makes the else invalid.
Get rid of the spurious semicolon you get
if (x)
for (y=x; y>0 && a[x]!=TARGET]; y--)
if (!(a[x]==BAD) && assertFailedOnL ine (THIS_FILE, __LINE__))
FatalExit (0);
else
y=-1;

which makes the else associate with the if from the assert macro. Given
the original definition you get

if (x)
for (y=x; y>0 && a[x]!=TARGET]; y--)
do { \
if (!(a[x]==BAD) && assertFailedOnL ine (THIS_FILE, __LINE__))
FatalExit (0);
} while (0);
else
y=-1;

which will behave as expected.

This is why you will see people using do { ... } while (0) in macros
when they want something that behaves like a function with a return type
of void. Also note the lack of a semicolon at the end of the macro
definition.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #4
ANNE Alexis wrote:
I don't know how it's call in english, so I try to explain it. It
because "effets de bord" tha are problem due to what we can find
just before an after the use of this macro.


Hello petit Jean Kevin de la porte d'Ivry :-)

"Effets de bord" is the (unfortunate) French translation for "side
effects". It should, instead, have been translated to "effets
secondaires".

Nov 13 '05 #5
Martin wrote:

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) \ #define ASSERT(f) \
if (!(f) && assertFailedOnL ine (THIS_FILE, __LINE__)) \
FatalExit (0); \


If you place a backslash as the last character
in the last line of a macro, then,
it is no longer the last line of the macro.

--
pete
Nov 13 '05 #6
On 2003-11-15, Martin <mr*****@totali se.co.uk> wrote:
#define ASSERT(f) \
do { \
if (!(f) && assertFailedOnL ine (THIS_FILE, __LINE__)) \
FatalExit (0); \
} while (0) \
vs...
#define ASSERT(f) \
if (!(f) && assertFailedOnL ine (THIS_FILE, __LINE__)) \
FatalExit (0); \

Is there any reason to use the first definition?


This is a comp.lang.c FAQ. Please read the answer to question 10.4 of
the C-faq.

http://www.eskimo.com/~scs/C-faq/top.html

-- James
--
The C-faq. Use for rubbing, massaging, and as an external stimulant.
Nov 13 '05 #7
In article <cb************ **************@ posting.google. com>,
mr*****@totalis e.co.uk (Martin) wrote:
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?


Try compiling this:

if (x >= 0)
ASSERT (x <= 10);
else
ASSERT (x >= -10);
Nov 13 '05 #8
Christian Bau wrote:
In article <cb************ **************@ posting.google. com>,
mr*****@totalis e.co.uk (Martin) wrote:
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?


Try compiling this:

if (x >= 0)
ASSERT (x <= 10);
else
ASSERT (x >= -10);


Another excellent argument for using { } every time.

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

if(x >= 0)
{
ASSERT(x <= 10);
}
else
{
ASSERT(x >= -10);
}

....works just fine without the do/while(0) silliness.

--
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 #9
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #10

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

Similar topics

7
3177
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
2047
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
7736
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
7271
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
3164
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
3102
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
5729
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
2189
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
9219
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
8997
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
8833
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,...
0
9389
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
9335
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,...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4709
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
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.