473,772 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assert

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)?
Nov 13 '05
21 7280
In <bf**********@h ercules.btinter net.com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
If you're not 100% sure that the program can run correctly without the
assertions, your program is not ready for production.


By this logic, complex programs, like compilers, would be *never*
released.

Real life constraints impose the release of programs that are not 100%
bug-free, simply because it cannot be demonstrated that they are 100%
bug-free with a reasonable amount of time and manpower resources.

The question is what to do with such programs: mask the bugs, so that
the user is not aware that he's getting the wrong results or expose them
to the user (when they can be internally detected). gcc's ICEs
(Internal Compiler Errors) are certainly the moral equivalent of asserts
left activated in production code. And I've gotten once a plain assertion
failure from the Digital Unix linker. In either case I was much happier
than when I had to discover an unreported toolchain error myself.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #11
Richard Heathfield wrote:

Derk Gwen wrote:
gi******@giusep pe.wwwew (Giuseppe) wrote:
# 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)?

The most diffult to debug, to understand problems occur in once the code
is in production, where it is constantly hammerred to its limits by people
who have no respect for your test suite.

Therefore in the interest of speed, production code is precisely when
you need to disable all checking, assertions, array bounds and other
ranges, stacks popped properly, etc. This ensures the maximum damage and
the least possibility of fixing it.


If you're not 100% sure that the program can run correctly without the
assertions, your program is not ready for production.


Corollary: No non-trivial program is ever ready for
production.

It seems to me that "ready for production" and "100%
bug-free" are not equivalent notions. A program is ready
for production when it is "good enough" for the application
at hand. A program may be "good enough" without being
"perfect" -- and certainly without satisfying the still
stronger condition of "proven perfect."

To put it another way: If unachievable perfection is the
criterion, the program will never be put into production.
The expected waiting time for the program to perform its
function is therefore infinite, and the ultimate result
(i.e., "nothing useful happens") can be achieved much more
quickly and cheaply by writing a program that crashes as
soon as it's invoked. In this sense, "100% bug-free" and
"100% bug-ridden" are operationally equivalent ;-)

Returning to the question of whether "production " code
should be built with or without NDEBUG defined, I seem to
recall PJ Plauger opining in "The Standard C Library" that
the problem with assert() in production code isn't that it
wastes cycles or some such, but that when it (inevitably)
*does* catch a problem the error message is gibberish to
the end user. It would seem to follow (although I don't
recall whether Plauger said so explicitly) that production
code should in fact test for its own correct operation, but
should use some nicer mechanism than the developer-oriented
bare assert().

There's also the experience some years back of the little
robot that landed on Mars, crawled around doing stuff, and
kept mysteriously freezing up and rebooting. The problem (a
classic priority inversion) was fixed only because the on-Mars
software (a "production " deployment if I've ever heard of one!)
still had its debugging probes activated; this facilitated
Earth-side analysis and provided a vehicle for uploading what
amounted to a patch. Looking back on the incident, a senior
software person on the project gave it as a mantra: "Test what
you fly, and fly what you test" -- in short, if you test the
code with debug probes in place, pulling out the probes leaves
you with a program that has seen *no* testing at all! Put the
former, not the latter, into production.

--
Er*********@sun .com
Nov 13 '05 #12
On 16 2003, go***********@s neaky.lerctr.or g (Gordon Burditt) wrote:
is assert() for debug only or not?


The purpose of a tool is determined by the person using it. It is
possible that someone out there uses assert() for driving nails and
uses pencils for murdering people.
Is it possible that I have seen the use of assert() in the Borland
c++ 32 compiler (so assert is not for debug only)?


Is it possible that you have seen the source code of the Borland
c++ 32 compiler? I don't know. If you have, whether or not it
uses assert() says little about whether or not assert() is for debug
only. Just because code is a production release does not mean it
is bug-free. However, I'm not sure I'd be comfortable riding in
an airplane or spacecraft whose flight-control software used assert(),
or living near a nuclear reactor whose control software used it.


living near a nuclear reactor is always not good
and there are programs that can't fail
but
where is the problem with assert() or with escise()?

#include <stdio.h>
#include <stdlib.h>
#define escise(a) fun(a, #a, __FILE__, __LINE__)

void fun(int b, char* a, char* c, unsigned d)
{if(b==0) return;
fflush(stdout);
if(!ferror(stde rr))
fprintf(stderr,
"Esco per %s in file: %s linea:%u Ciao\n", a, c, d);
fflush(stderr);
abort(); /* Is the problem here? */
}
int main()
{int *a=0;

escise(a==0);
printf("fuori uno\n");
return 0;
}

Thanks
_______
Sistema maggioritario== DITTATURA
Sistema proporzionale== democrazia
Nov 13 '05 #13

"Eric Sosman" <Er*********@su n.com> wrote in message

It seems to me that "ready for production" and "100%
bug-free" are not equivalent notions. A program is ready
for production when it is "good enough" for the application
at hand.

How many people would spend ten times as much money on a car that was
designed so that it never broke down?
Nov 13 '05 #14

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message

The C preprocessor assert macro is almost never
the best way to detect and handle exceptions so it is not used
for exception handling by professional programmers.

It is used to detect an error by the calling programmer.

eg

double squareroot(doub le x)
{
assert(x >= 0);
...
}

However not

printf("Input number you want the square root of\n");
scanf("%f\n", &x);
assert(x >= 0);
Nov 13 '05 #15
Eric Sosman wrote:

<snip>
Returning to the question of whether "production " code
should be built with or without NDEBUG defined, I seem to
recall PJ Plauger opining in "The Standard C Library" that
the problem with assert() in production code isn't that it
wastes cycles or some such, but that when it (inevitably)
*does* catch a problem the error message is gibberish to
the end user.
That is indeed a serious problem.

It would seem to follow (although I don't recall whether Plauger said so explicitly) that production
code should in fact test for its own correct operation, but
should use some nicer mechanism than the developer-oriented
bare assert().
And that brings you right back to the old trade-off between speed and
"security".
There's also the experience some years back of the little
robot [...] Looking back on the incident, a senior
software person on the project gave it as a mantra: "Test what
you fly, and fly what you test" -- in short, if you test the
code with debug probes in place, pulling out the probes leaves
you with a program that has seen *no* testing at all! Put the
former, not the latter, into production.


I have no problem with that idea, but I would recommend not using assert()
in that situation. Rather, recover gracefully from the error if possible,
and in any event provide meaningful information to the user. Assertions
were not designed to do 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
>>The purpose of a tool is determined by the person using it. It is
possible that someone out there uses assert() for driving nails and
uses pencils for murdering people.
Is it possible that I have seen the use of assert() in the Borland
c++ 32 compiler (so assert is not for debug only)?
Is it possible that you have seen the source code of the Borland
c++ 32 compiler? I don't know. If you have, whether or not it
uses assert() says little about whether or not assert() is for debug
only. Just because code is a production release does not mean it
is bug-free. However, I'm not sure I'd be comfortable riding in
an airplane or spacecraft whose flight-control software used assert(),
or living near a nuclear reactor whose control software used it.


living near a nuclear reactor is always not good


That depends on how much it leaks and how well it is designed, and
how close "near" is. I'd much rather live 50 miles from a reactor
programmed to shut down in case of an emergency, with multiple
fail-safes that it can do so, than live 500 miles from one that's
going to shut down the control computer with the control rods still
set for full-blast power generation if one of the sensors says
something it doesn't like, and in all probability cause a core
meltdown.
and there are programs that can't fail
Really? I doubt that there is anything a program can run on
that can't fail, and reality intrudes on ANSI C with things
like the impossibility of guaranteeing that there will be no
power failures or nuclear EMP while the program is running.
but
where is the problem with assert() or with escise()?
Generally:

The higher-level routines of the program don't have a chance
to clean up (delete temporary files, finish half-written files, etc.)
or actually try to RECOVER from the error.
The error message generally has all the clarity of
"Shut 'er down Scotty, she's sucking mud again" (to anyone recognizing
that message: what did it REALLY mean?) especially to someone to
whom you did not sell the source code but sold the binary. It
generally does not report possibly-relevant info (for your function
below, what WAS the value of b? It's not 0, but what was it? This
might be relevant debugging info.)
If the program is in the middle of doing something physical
and important, say, flying an airplane, just halting tends to lead
to catastrophic consequences.

Gordon L. Burditt
#include <stdio.h>
#include <stdlib.h>
#define escise(a) fun(a, #a, __FILE__, __LINE__)

void fun(int b, char* a, char* c, unsigned d)
{if(b==0) return;
fflush(stdout);
if(!ferror(stde rr))
fprintf(stderr,
"Esco per %s in file: %s linea:%u Ciao\n", a, c, d);
fflush(stderr);
abort(); /* Is the problem here? */
}
int main()
{int *a=0;

escise(a==0);
printf("fuori uno\n");
return 0;
}

Thanks

Nov 13 '05 #17
E. Robert Tisdale wrote:
The C preprocessor assert macro is almost never
the best way to detect and handle exceptions
Of course not. That's like saying getchar() is almost never the best way to
calculate the square root of a number. It's a self-evident truth of no
intrinsic value.
so it is not used
for exception handling by professional programmers.


Of course not. It's a program error detection facility, not an exception
handler.

--
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
Giuseppe wrote:
Is assert() for debug only or not?


ASSERT(3) Linux Programmer’s Manual ASSERT(3)

NAME
assert - abort the program if assertion is false

SYNOPSIS
#include <assert.h>

void assert(scalar expression);

DESCRIPTION
If the macro NDEBUG was defined at the moment <assert.h> was last
included, the macro assert() generates no code, and hence does
nothing at all. Otherwise, the macro assert() prints an error
message to standard output and terminates the program by calling
abort() if expression is false (i.e., compares equal to zero).

The purpose of this macro is to help the programmer find bugs in
his program. The message "assertion failed in file foo.c,
function do_bar(), line 1287" is of no help at all to a user.

RETURN VALUE
No value is returned.

CONFORMING TO
ISO9899 (ANSI C). In the 1990 standard, expression is required
to be of type int and undefined behavior results if it is not,
but in the 1999 standard it may have any scalar type.

BUGS
assert() is implemented as a macro; if the expression tested has
side- effects, program behaviour will be different depending on
whether NDEBUG is defined. This may create Heisenbugs which
go away when debugging is turned on.

SEE ALSO
exit(3), abort(3), assert_perror(3 )

C99 2002-08-25 ASSERT(3)

Nov 13 '05 #19
In 'comp.lang.c', Zoran Cutura <zo**********@d aimlerchrysler. com> wrote:
you want to compile with NODEBUG defined, so that all assertions will be
gone.


ITYM 'NDEBUG'

--
-ed- em**********@no os.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #20

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

Similar topics

28
3596
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 this piece of code is broken, but I think the idea is very obvious. In Ruby, assert is simply a...
3
3967
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") >>>
27
3860
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 seemingly
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
3115
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
5735
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?
13
9430
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 when the expression is wrong. For eg.,
12
1928
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.
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);
9
21491
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
9620
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
10261
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...
0
10104
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
10038
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
9912
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
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.