473,563 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assert-macro: brackets

Leo
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__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)
Jul 22 '05 #1
4 2037
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@emai l.com> wrote:
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__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)


No, try the following with your two versions of assert

if (0 && assert(0))
{
}

With the first definition this will not call _assert (which is correct),
with the second definition it will.

john
Jul 22 '05 #2
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@emai l.com> wrote:
....
No, try the following with your two versions of assert

if (0 && assert(0))
I think you meant

if (0 && assert(1))
{
}

With the first definition this will not call _assert (which is
correct), with the second definition it will.


G
Jul 22 '05 #3
Leo
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@emai l.com> wrote:
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__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)

No, try the following with your two versions of assert

if (0 && assert(0))
{
}

With the first definition this will not call _assert (which is
correct), with the second definition it will.

john


Both versions won't compile :D
Jul 22 '05 #4
On Sun, 25 Jul 2004 00:38:29 +0200, Leo <le*******@emai l.com> wrote:
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@emai l.com> wrote:
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__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)

No, try the following with your two versions of assert
if (0 && assert(0))
{
}
With the first definition this will not call _assert (which is
correct), with the second definition it will.
john


Both versions won't compile :D


Yes, I realised that after I posted.

How about this?

1 && assert(0);

That fails to compile with version 1, but compiles with version 2.

I'm sure a better example could be devised, but the point is that the
extra bracket in the first version avoids any surprises.

john
Jul 22 '05 #5

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

Similar topics

28
3548
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a very early draft for a new "assert" syntax: This was inspired in Ruby's assert syntax. I'm not familiar with Ruby at all, so the chances are that...
3
3955
by: Thomas Guettler | last post by:
Hi, Python 2.3.3 (#1, Feb 5 2005, 16:22:10) on linux2 >>> assert 0, "foo" Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError: foo >>> assert(0, "foo") >>>
12
6652
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that assert could be used to start an interactive debugger automatically. How do I realize that on a Linux machine using gcc?
27
3810
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above...
5
3152
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
3068
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...
2
5856
by: cody | last post by:
System.Diagnostics.Debug.Assert(); Hello??? A language should encourage programmers to heavily use the assert-feature, since it improves safety, stability, readability and maintainability of software. Most language designers recognized this, see C/C++ and Java which supports an assert keyword. My question now is, will C# support in future...
13
9399
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could show me the actual source code of assert() function in assert.h header file. I need to write the assert function that prints out error message...
29
2484
by: mailforpr | last post by:
Sometimes, I can't think of any good reason why I should have the program's logic thrown an exception. Except for catching the exception and printing "Uh, oh" to the screen. I also think that in most cases there's simply no way to handle an exception properly, because what can one do about an integer overflow? Reassign values? Restart the...
9
21455
by: pereges | last post by:
Ok, so once I'm done debugging my code(split across multiple modules) using the assert macro, I would want to switch off all the assert macros ued in the program. Does this mean I have to include: #define NDEBUG in every .c where I used assert or defining it one file would turn off all assert macros in every file ?
0
7664
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...
0
8106
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...
0
7948
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...
0
6250
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...
1
5484
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...
0
5213
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...
0
3642
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...
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
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...

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.