473,766 Members | 2,023 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 14128
John Devereux wrote:
What is wrong with simply

if(x) y();

???


Syntactically, nothing. In practice, there are two reasons why I don't do
this.

Firstly, when I'm stepping through code with one of those new-fangled
debuggers, it's much easier to see whether y() is called if it's on a
separate line.

Secondly (and this applies equally to the multi-line version of the same
code), Murphy's Law states quite clearly that, if I use a single-statement
if() now, I will inevitably, and probably at some point in the very near
future, think of something else that needs to be done in the same block, so
I might as well make it a compound statement right now.

--
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 #21
Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
John Devereux wrote:
What is wrong with simply

if(x) y();

???
Syntactically, nothing. In practice, there are two reasons why I don't do
this.

Firstly, when I'm stepping through code with one of those new-fangled
debuggers, it's much easier to see whether y() is called if it's on a
separate line.


Yes, that's true. Similarly, I suppose your form makes it easier to
locate the offending line when you get a compiler diagnostic.
Secondly (and this applies equally to the multi-line version of the same
code), Murphy's Law states quite clearly that, if I use a single-statement
if() now, I will inevitably, and probably at some point in the very near
future, think of something else that needs to be done in the same block, so
I might as well make it a compound statement right now.


No. Never do today what can be put off until tomorrow :)
--

John Devereux
Nov 13 '05 #22
Richard Heathfield wrote:
.... 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.)


Which is why my compromise, not rigorously adhered to, is:

if (w != x) y();
vs
if (w != x) {
y();
z();
}

This removes the temptation to squeeze in a line somewhere. It
also does not exacerbate the newline shortage.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #23
CBFalconer wrote:
Richard Heathfield wrote:

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


Which is why my compromise, not rigorously adhered to, is:

if (w != x) y();
vs
if (w != x) {
y();
z();
}

This removes the temptation to squeeze in a line somewhere. It
also does not exacerbate the newline shortage.


No shortage here in the UK. In the interests of cross-pond amity, I present
you with a newline generator:

#include <stdio.h>
{
int i = 0;
while(i++ < 1000)
{
puts("\n\n\n\n\ n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\ n");
}
return 0;
}

You can redirect to a file, of course. If you use them all up, just run it
again.

No charge. God Bless America.

--
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 #24
On Sun, 23 Nov 2003 02:34:01 -0500, Sheldon Simms
<sh**********@y ahoo.com> wrote:
I have
never seen a single case in which a programmer actually had any problem
with this style


I have. I don't know how often it happens, because I only see those
cases where the bug doesn't become obvious immediately and I have to
fix it.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #25
In <vf**********@d evereux.me.uk> John Devereux <jd*@devereux.m e.uk> writes:
Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
John Devereux wrote:
What is wrong with simply

if(x) y();

???


Syntactically, nothing. In practice, there are two reasons why I don't do
this.

Firstly, when I'm stepping through code with one of those new-fangled
debuggers, it's much easier to see whether y() is called if it's on a
separate line.


Yes, that's true. Similarly, I suppose your form makes it easier to
locate the offending line when you get a compiler diagnostic.


If this is an issue at all (not everybody is a debugger junkie), there is
a simpler solution:

if (x)
y();

i.e. exactly the way you'd do it if x were a fairly long expression and/or
the y() function had a lot of parameters.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #26
Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:
No shortage here in the UK. In the interests of cross-pond amity, I present
you with a newline generator: (snipped)


You do seem to be short on semicolons however, because apparently you
didn't have enough to both write proper code and supply a winky-face
to indicate that the post was tongue-in-cheek. I'd give you a few,
but unfortunately I don't even have one to winky-face this post, never
mind yours. It seems, then, that we will sound much terser than we
really are, at least until China begins exporting cheap semicolons.

--
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 #27
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:
No shortage here in the UK. In the interests of cross-pond amity, I present
you with a newline generator:
(snipped)

<unsnipped>
#include <stdio.h>
{
int i = 0;
while(i++ < 1000)
{
puts("\n\n\n\n\ n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\ n");
}
return 0;
}
You do seem to be short on semicolons however, because apparently you
didn't have enough to both write proper code and supply a winky-face
to indicate that the post was tongue-in-cheek.
As it turns out Richard isn't short on semicolons but on
'int main(void)'s. I can offer him some of my spare ones:

int main(void) int main(void) int main(void) int main(void)
int main(void) int main(void) int main(void) int main(void)

BTW: I still have a whole lot of 'void main(void)'s in a box
in my cellar, left over from my first attempts in writing C
programs; is anybody interested? Free of charge, delivery via
email...
I'd give you a few,
but unfortunately I don't even have one to winky-face this post, never
mind yours. It seems, then, that we will sound much terser than we
really are, at least until China begins exporting cheap semicolons.


Coincidentally I just received a box of brand new premium semicolons
today; here you go:

;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;
;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;
;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;
;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;

If you need more, just email me. ;-)
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #28
Irrwahn Grausewitz <ir*******@free net.de> spoke thus:
Coincidentally I just received a box of brand new premium semicolons
today; here you go: ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;
;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;
;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;
;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;


Great, thanks. My winky faces will look stupdendous now ;)

--
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 #29
On Wed, 26 Nov 2003 15:41:18 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:
No shortage here in the UK. In the interests of cross-pond amity, I present
you with a newline generator:

(snipped)


You do seem to be short on semicolons however, because apparently you
didn't have enough to both write proper code and supply a winky-face
to indicate that the post was tongue-in-cheek.


I didn't need a "winky-face" to categorize that post ;-)

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #30

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

Similar topics

7
3180
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
2049
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
7737
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
7277
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
3165
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
3110
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
5734
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
2196
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
9220
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
9568
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
10168
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...
1
9959
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
9837
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
8833
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
6651
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();...
1
3929
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
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.