473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

using assert

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
assert() evalutates to 0, then the name of the source file and the number of
the line on which assert() appears is displayed, and the execution of the
program is aborted by calling abort()"

If I want to use this macro I have to define it in this way
#define NDEBUG
asser(expression);
#endif

So if you want to use this NDEBUG macro I have to manually define or
undefine it.

So what the text say must be completely wrong.

//Tony
Aug 17 '05 #1
3 7715
Tony Johansson wrote:

If I want to use this macro I have to define it in this way
#define NDEBUG
asser(expression);

So if you want to use this NDEBUG macro I have to manually define or
undefine it.

So what the text say must be completely wrong.


The text is muddled. The rule is that if the macro NDEBUG is defined at
the point of the #include <assert.h> directive, the assert macro has no
effect. If the macro NDEBUG is not defined at that point, the assert
macro displays a message (which includes the file name, line number, and
the text of the expression) and calls abort(). Like this:

#include <assert.h>

void f()
{
assert(0); // aborts if called
}

#define NDEBUG
#include <assert.h>

void g()
{
assert(0); // no effect
}

#undef NDEBUG
#include <assert.h>

void h()
{
assert(0); // aborts if called
}

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 17 '05 #2
"Pete Becker" <pe********@acm.org> wrote in message
news:1P********************@rcn.net...
The text is muddled. The rule is that if the macro NDEBUG is defined at
the point of the #include <assert.h> directive, the assert macro has no
effect. If the macro NDEBUG is not defined at that point, the assert
macro displays a message (which includes the file name, line number, and
the text of the expression) and calls abort().


Also, most C++ compilers give you a way to specify an initial value for
NDEBUG as part of compiling your program, rather than having to modify your
source code. So if you are using such a compiler (and most of us are), you
can leave NDEBUG off during testing and turn it on for production to make
your code run faster.
Aug 17 '05 #3
Andrew Koenig wrote:

Also, most C++ compilers give you a way to specify an initial value for
NDEBUG as part of compiling your program, rather than having to modify your
source code. So if you are using such a compiler (and most of us are), you
can leave NDEBUG off during testing and turn it on for production to make
your code run faster.


Good point.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 18 '05 #4

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

Similar topics

28
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...
3
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") >>>
21
by: kimimaro | last post by:
Is there anymore methods in exiting your program using pure C language other than return 0?
5
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; }
4
by: cmrchs | last post by:
Hi, I created some WebPermissions : Regex myRegex = new Regex(@"http://www.linux.org/"); WebPermission myWebPermission = new WebPermission(NetworkAccess.Connect,myRegex); string URL2 =...
28
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,...
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
2
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not...
12
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I saw a couple of form of assert in code on Windows, 1. ASSERT; 2. assert; 3. _ASSERT; 4. _assert.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.