473,408 Members | 1,871 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,408 software developers and data experts.

"Safer" assert?

I've been using assert liberally throughout my code.
Then, upon compiling with -NDEBUG, I found that my program had
different output.
Why? Because -NDEBUG disables assert, but I had (at least) one assert
with a side-effect.

Can someone recommend a safer mechanism for assertions? e.g. one that
determines the const-ness of what is being checked?

Thanks,
Joseph

Nov 8 '06 #1
6 1508

Joseph Turian wrote:
I've been using assert liberally throughout my code.
Then, upon compiling with -NDEBUG, I found that my program had
different output.
Why? Because -NDEBUG disables assert, but I had (at least) one assert
with a side-effect.
It's not a good idea for an assert to have a side-effect that affects
the behaviour of your program for precisely the reason you have
discovered.

Can someone recommend a safer mechanism for assertions? e.g. one that
determines the const-ness of what is being checked?
Can you show some example code that illustrates what you are trying to
do?

Gavin Deane

Nov 8 '06 #2
Joseph Turian:
I've been using assert liberally throughout my code.

Brilliant.

Then, upon compiling with -NDEBUG, I found that my program had
different output.
Why? Because -NDEBUG disables assert, but I had (at least) one assert
with a side-effect.

Can someone recommend a safer mechanism for assertions? e.g. one that
determines the const-ness of what is being checked?

First of all, there's nothing wrong with "assert". The only problem is your
malusage of it.

Here's a quick idea to find the asserts which have side-effects.

Firstly, make a copy of the entire project, i.e. all the code files. Do a
"Find and Replace", replacing each occurence of "assert" with "assertBAD".
Compile the program and you should get a list of lines on which the
unidentified identifier "assertBAD" was used. Save the compiler output to a
text file (i.e. the file and line number errors).

Now, use your compiler options to define the following macro across the
entire program:

#define assertBAD(expr) expr

Now, re-compile the program. If you have the appropriate warning level set,
the compiler will warn you of any redundant expression-statements. Here's
an example of a Before and After:

Before:

int main()
{
assert(i % 10);
}

After:

int main()
{
i % 10;
}

The compiler can see plainly that the expression-statement is useless, and
will warn you. Again, save the compiler output to a file.

Now compare the two lists. The ones that are on the first list but which
aren't on the second are the ones which have side-effects. Of course you'll
get a few false-positives such as:

UserDefinedType obj;

assert(obj.IsOpen());

, but hopefully they'll be in the minority.

--

Frederick Gotham
Nov 8 '06 #3

Joseph Turian wrote:
I've been using assert liberally throughout my code.
Then, upon compiling with -NDEBUG, I found that my program had
different output.
Why? Because -NDEBUG disables assert, but I had (at least) one assert
with a side-effect.

Can someone recommend a safer mechanism for assertions? e.g. one that
determines the const-ness of what is being checked?
No. Nothing you can do will keep you from shooting yourself in the
head in this manner. Anything that could be remotely considered could
be worked around since even a const call on a const object could have
side effects.

Nov 8 '06 #4
Joseph Turian wrote:
...
Can someone recommend a safer mechanism for assertions? e.g. one that
determines the const-ness of what is being checked?
...
Assertion with side effect is not something unusual, as long as this
side effect is only needed in the DEBUG version of the program (i.e. is
there for the sole purpose of supporting this and other assertions).
'assert', of course, does not and cannot know whether each specific side
effect is critical to the correct behavior of the NDEBUG version. I
don't think it is possible to implement a side-effect-aware version of
'assert' without a dedicated support from the compiler.

--
Best regards,
Andrey Tarasevich

Nov 9 '06 #5
Joseph Turian <tu****@gmail.comwrote:
I've been using assert liberally throughout my code.
Then, upon compiling with -NDEBUG, I found that my program had
different output.
Why? Because -NDEBUG disables assert, but I had (at least) one assert
with a side-effect.

Can someone recommend a safer mechanism for assertions? e.g. one that
determines the const-ness of what is being checked?
Well, on comp.lang.c++.moderated there was this (somewhat lengthy)
thread a little bit ago about the VERIFY(expr) macro, which is intended
to act like assert(expr) in debug mode, but still evaluates expr in
release mode.

http://groups.google.com/group/comp....034283152115e/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 9 '06 #6
Joseph Turian wrote:
I've been using assert liberally throughout my code.
Then, upon compiling with -NDEBUG, I found that my program had
different output.
Why? Because -NDEBUG disables assert, but I had (at least) one assert
with a side-effect.
Can someone recommend a safer mechanism for assertions? e.g. one that
determines the const-ness of what is being checked?
I think assert has accomplished his goal in this case, you have found a bug,
it isn't?

--
Salu2
Nov 10 '06 #7

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

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
27
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
11
by: s99999999s2003 | last post by:
hi i have a file with xxx.xxx.xxx.xxx:yyy xxx.xxx.xxx.xxx:yyy xxx.xxx.xxx.xxx:yyy xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx:yyy
2
by: jain_tj | last post by:
Could anyone please help me with the following problem My xml file is ============== <fig id="F0000001"> <caption>Caption text</caption> <image id="I0000001" image.class="halftone"...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
9
by: Ben Bacarisse | last post by:
I am porting a program from the Windows world to the Linux world. The source uses MS's new "safer" string functions such as: strcat_s(dest, size, source); but there are also calls such as: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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...

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.