Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > assert in C

Reply
Thread Tools

assert in C

 
 
cuiyouzhi0000@gmail.com
Guest
Posts: n/a
 
      12-25-2009
Dear all,

I wrote a C program with assert ( str != NULL ) to check char * str is
NULL or not.

then I transferred str with NULL to assert, and I used gcc -g ... to
compile it, sure, I got
assertion.

While,
1. When I used gcc -O to compile the same program, I got the assertion
as well.
2. When I added #define NDEBUG after #include<assert.h>, used gcc -g
to compile the program,
I got the assertion as well.

I knew, if we used -O or define NDEBUG, then assert statement will not
be run. but seems I am wrong or someone was wrong?

Anyone can help me?

 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      12-25-2009
On 2009-12-25, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> While,
> 1. When I used gcc -O to compile the same program, I got the assertion
> as well.
> 2. When I added #define NDEBUG after #include<assert.h>, used gcc -g
> to compile the program,
> I got the assertion as well.


"after"

> I knew, if we used -O or define NDEBUG, then assert statement will not
> be run. but seems I am wrong or someone was wrong?


Try defining NDEBUG *before* including <assert.h>.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
cuiyouzhi0000@gmail.com
Guest
Posts: n/a
 
      12-25-2009
On Dec 25, 8:06*pm, Seebs <(E-Mail Removed)> wrote:
> On 2009-12-25, (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> > While,
> > 1. When I used gcc -O to compile the same program, I got the assertion
> > as well.
> > 2. When I added #define NDEBUG after #include<assert.h>, used gcc -g
> > to compile the program,
> > I got the assertion as well.

>
> "after"
>
> > I knew, if we used -O or define NDEBUG, then assert statement will not
> > be run. but seems I am wrong or someone was wrong?

>
> Try defining NDEBUG *before* including <assert.h>.
>
> -s
> --
> Copyright 2009, all wrongs reversed. *Peter Seebach / (E-Mail Removed)://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


thanks, you are right, after I defined the #define NDEBUG before
#include<assert.h>, assert statement doesn't work yet.

while, how about the -O option in gcc? can this make assert not
workable?
 
Reply With Quote
 
Michael Tsang
Guest
Posts: n/a
 
      12-25-2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(E-Mail Removed) wrote:

> On Dec 25, 8:06 pm, Seebs <(E-Mail Removed)> wrote:
>> On 2009-12-25, (E-Mail Removed) <(E-Mail Removed)> wrote:
>>
>> > While,
>> > 1. When I used gcc -O to compile the same program, I got the assertion
>> > as well.
>> > 2. When I added #define NDEBUG after #include<assert.h>, used gcc -g
>> > to compile the program,
>> > I got the assertion as well.

>>
>> "after"
>>
>> > I knew, if we used -O or define NDEBUG, then assert statement will not
>> > be run. but seems I am wrong or someone was wrong?

>>
>> Try defining NDEBUG *before* including <assert.h>.
>>
>> -s
>> --
>> Copyright 2009, all wrongs reversed. Peter Seebach /
>> (E-Mail Removed)://www.seebs.net/log/<-- lawsuits, religion,
>> and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology)
>> <-- get educated!

>
> thanks, you are right, after I defined the #define NDEBUG before
> #include<assert.h>, assert statement doesn't work yet.
>
> while, how about the -O option in gcc? can this make assert not
> workable?


The -O option in gcc must not change the behaviour of a program (unless it
has undefined or unspecified behaviour).
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAks0xggACgkQG6NzcAXitM+uOwCfQWH0/eH9IO/EyN7XvmRfpx18
kigAn3aILVbsQrHjZZXMRFle/wrQiiGl
=ewEl
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-25-2009
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> On Dec 25, 8:06*pm, Seebs <(E-Mail Removed)> wrote:
>> On 2009-12-25, (E-Mail Removed) <(E-Mail Removed)> wrote:
>>
>> > While,
>> > 1. When I used gcc -O to compile the same program, I got the assertion
>> > as well.
>> > 2. When I added #define NDEBUG after #include<assert.h>, used gcc -g
>> > to compile the program,
>> > I got the assertion as well.

>>
>> "after"
>>
>> > I knew, if we used -O or define NDEBUG, then assert statement will not
>> > be run. but seems I am wrong or someone was wrong?

>>
>> Try defining NDEBUG *before* including <assert.h>.

>
> thanks, you are right, after I defined the #define NDEBUG before
> #include<assert.h>, assert statement doesn't work yet.
>
> while, how about the -O option in gcc? can this make assert not
> workable?


No, the "-O" option doesn't affect the behavior of assert. Why did
you think it would?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
cuiyouzhi0000@gmail.com
Guest
Posts: n/a
 
      12-26-2009
On Dec 26, 4:51*am, Keith Thompson <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> writes:
> > On Dec 25, 8:06*pm, Seebs <(E-Mail Removed)> wrote:
> >> On 2009-12-25, (E-Mail Removed) <(E-Mail Removed)> wrote:

>
> >> > While,
> >> > 1. When I used gcc -O to compile the same program, I got the assertion
> >> > as well.
> >> > 2. When I added #define NDEBUG after #include<assert.h>, used gcc -g
> >> > to compile the program,
> >> > I got the assertion as well.

>
> >> "after"

>
> >> > I knew, if we used -O or define NDEBUG, then assert statement will not
> >> > be run. but seems I am wrong or someone was wrong?

>
> >> Try defining NDEBUG *before* including <assert.h>.

>
> > thanks, you are right, after I defined the #define NDEBUG before
> > #include<assert.h>, assert statement doesn't work yet.

>
> > while, how about the -O option in gcc? can this make assert not
> > workable?

>
> No, the "-O" option doesn't affect the behavior of assert. *Why did
> you think it would?
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) *<http://www.ghoti.net/~kst>
> Nokia
> "We must do something. *This is something. *Therefore, we must do this."
> * * -- Antony Jay and Jonathan Lynn, "Yes Minister"


OK, then if I compile the c program in release mode, the assert
statement should not workable, is it wrong?
If it is wrong, then how to disable the assert statement by gcc?
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      12-26-2009
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> On Dec 26, 4:51*am, Keith Thompson <(E-Mail Removed)> wrote:
>> "(E-Mail Removed)" <(E-Mail Removed)> writes:
>> > On Dec 25, 8:06*pm, Seebs <(E-Mail Removed)> wrote:
>> >> On 2009-12-25, (E-Mail Removed) <(E-Mail Removed)> wrote:

>>
>> >> > While,
>> >> > 1. When I used gcc -O to compile the same program, I got the assertion
>> >> > as well.
>> >> > 2. When I added #define NDEBUG after #include<assert.h>, used gcc -g
>> >> > to compile the program,
>> >> > I got the assertion as well.

>>
>> >> "after"

>>
>> >> > I knew, if we used -O or define NDEBUG, then assert statement will not
>> >> > be run. but seems I am wrong or someone was wrong?

>>
>> >> Try defining NDEBUG *before* including <assert.h>.

>>
>> > thanks, you are right, after I defined the #define NDEBUG before
>> > #include<assert.h>, assert statement doesn't work yet.

>>
>> > while, how about the -O option in gcc? can this make assert not
>> > workable?

>>
>> No, the "-O" option doesn't affect the behavior of assert. *Why did
>> you think it would?


[Keith's signature snipped]
>
> OK, then if I compile the c program in release mode, the assert
> statement should not workable, is it wrong?
> If it is wrong, then how to disable the assert statement by gcc?


Can you avoid quoting people's signatures please?

That's right (although I remember heated debates about whether removing
asserts from releases is a good idea or not). But the way you do it is
not by changing optimisation level (I develop at high optimisation
levels - partly because of the better checking I get from the compiler -
only turning it down if debugging goes funny). You do it just the way
you've shown, by defining NDEBUG. You can do that with a compiler
option, you don't have to wire it into the program (-DNDEBUG for gcc).
--
Online waterways route planner: http://canalplan.org.uk
development version: http://canalplan.eu
 
Reply With Quote
 
cuiyouzhi0000@gmail.com
Guest
Posts: n/a
 
      12-26-2009
On Dec 26, 10:31*pm, Richard <(E-Mail Removed)> wrote:

> > Can you avoid quoting people's signatures please?


OK, thanks


>
> > That's right (although I remember heated debates about whether removing
> > asserts from releases is a good idea or not). *But the way you do it
> > is

>
> It is. asserts suck and are the sign of sloppy programmers who only use
> them to appear to be concerned about reliability. An assert condition
> should simply not occur.


I don't agree that assert condition should simply not occur.
such as, assert NULL pointer, I think it can occur at high
possibility.

>
> > not by changing optimisation level (I develop at high optimisation
> > levels - partly because of the better checking I get from the compiler -
> > only turning it down if debugging goes funny). *You do it just the way
> > you've shown, by defining NDEBUG. *You can do that with a compiler
> > option, you don't have to wire it into the program (-DNDEBUG for gcc).

>


So, gcc -NDEBUG -g *.c *.c -o * will work without assert?

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-26-2009
On 12/26/2009 10:27 AM, (E-Mail Removed) wrote:
> On Dec 26, 10:31 pm, Richard<(E-Mail Removed)> wrote:
>[...]
>> It is. asserts suck and are the sign of sloppy programmers who only use
>> them to appear to be concerned about reliability. An assert condition
>> should simply not occur.

>
> I don't agree that assert condition should simply not occur.
> such as, assert NULL pointer, I think it can occur at high
> possibility.


The claim that "asserts suck" sucks, but their purpose must
be properly understood: They are useful to the programmer, but
not to the end user. They can alert the programmer to programming
errors, but are not good for things like validating user input.

Since an unsuccessful assert() terminates the program (unless
you're doing tricky things with SIGABRT), an assert() that fires
with "high probability" means the program dies with that same
high probability -- which indicates that the program is not very
useful, because it keeps dying!

> So, gcc -NDEBUG -g *.c *.c -o * will work without assert?


The command line as shown will probably not work at all.
Changing it to something like "gcc -DNDEBUG -g *.c" will cause
the NDEBUG macro to be defined at the start of each .c file's
compilation. If it's still defined when <assert.h> is included
(that is, if you don't #undef it first), the assert() calls in
that file will expand to no-ops that have no effect on the
program's execution.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      12-26-2009
In article <hh5e0u$kbf$(E-Mail Removed)-september.org>,
Richard <(E-Mail Removed)> wrote:
....
>Asserts suck because of the termination. In 99% of test cases where that
>assert triggers you can also merely run the program in a debugger and
>get a backtrace there and then and "live debug". Asserts, like
>littering code with printfs, is amateur and sucks in most cases I have
>come across them polluting the code.


Indeed. Asserts, like the regs in this newsgroup, are fossils left over
from a day and age where debuggers didn't exist.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To assert or not to assert... ImpalerCore C Programming 79 05-17-2010 12:47 PM
Debug.Assert(): missing MsgBox (VS.NET 2003, C#) Mark Newby ASP .Net 1 05-03-2005 07:43 AM
assert 0, "foo" vs. assert(0, "foo") Thomas Guettler Python 3 02-23-2005 07:53 PM
assert(x) and '#define ASSERT(x) assert(x)' Alex Vinokur C Programming 5 11-25-2004 08:48 PM
RE: remove assert statement (Was: Re: PEP new assert idiom) Robert Brewer Python 1 11-07-2004 06:53 PM



Advertisments