Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ASSERT(something) what's it really mean

0 views
Skip to first unread message

Thomas Bonnes

unread,
Mar 27, 1997, 3:00:00 AM3/27/97
to

Hi,

I know this is a very simple topic, but can someone tell me more of
what assertions actually are? I see many examples in the good number
of books that I have, and even a few lame attempts to explain to me
what they exactly are used for( for example ::: when, why, how,
etc...... do you use ASSERT(something) statements ). Do assertions
just stop a function execution line path of code or do they do much
more than that. And where does using them gain you the most value in
programs?

Also what is the difference between VERIFY(something) and
ASSERT(something)? I guess VERIFY is just used in Release builds for
the most part(just a guess here).

thanks,

Tom Bonnes

send by email also if you have the option to send both ways in your
mail client if you will so kind.


Chris Marriott

unread,
Mar 27, 1997, 3:00:00 AM3/27/97
to

In article <5he7ov$p...@topgun.es.dupont.com>, Thomas Bonnes <bonnestj@a1
.esvax.umc.dupont.com> writes

> I know this is a very simple topic, but can someone tell me more of
>what assertions actually are? I see many examples in the good number
>of books that I have, and even a few lame attempts to explain to me
>what they exactly are used for( for example ::: when, why, how,
>etc...... do you use ASSERT(something) statements ). Do assertions
>just stop a function execution line path of code or do they do much
>more than that. And where does using them gain you the most value in
>programs?

"ASSERT" is used to catch bugs in a debug build. Its argument is an
expression which must evaluate to TRUE. If any other result occurs, the
program will be terminated.

Eg, if you have an expression involving a pointer and you know that,
with correct operation, the pointer must never be NULL, you could say:

ASSERT( p!=NULL );
if (*p.....)

>
>
>
> Also what is the difference between VERIFY(something) and
>ASSERT(something)? I guess VERIFY is just used in Release builds for
>the most part(just a guess here).

ASSERT completely disappears in a release build; VERIFY doesn't.

Chris

----------------------------------------------------------------
Chris Marriott, SkyMap Software, U.K. e-mail: ch...@skymap.com
Creators of fine astronomy software for Windows.
For full details, visit our web site at http://www.skymap.com


Al Margheim

unread,
Mar 29, 1997, 3:00:00 AM3/29/97
to

Thomas Bonnes <bonn...@a1.esvax.umc.dupont.com> wrote in article
<5he7ov$p...@topgun.es.dupont.com>...
: Hi,
:
: I know this is a very simple topic, but can someone tell me more of

: what assertions actually are? I see many examples in the good number
: of books that I have, and even a few lame attempts to explain to me
: what they exactly are used for( for example ::: when, why, how,
: etc...... do you use ASSERT(something) statements ). Do assertions
: just stop a function execution line path of code or do they do much
: more than that. And where does using them gain you the most value in
: programs?
:

Different people use ASSERT in different ways, so there isn't one right
answer to your question. I often use it for error checking when the chance
of an error is very small, but still possible. For example, many Win32
programmers don't bother to check the return value of PostMessage because
the Win32 PostMessage rarely fails and it's probably not worth the overhead
to add a lot of errorhandling code for that situation. ASSERT allows you to
check this condition in a debug version of your executable with very little
overhead. Just add one line of code and ASSERT will notify you when and if
the condition occurs.

Another use is to notify the programmer when someone is passing invalid
parameters to a function. MFC uses ASSERT for this purpose a lot.


: Also what is the difference between VERIFY(something) and


: ASSERT(something)? I guess VERIFY is just used in Release builds for
: the most part(just a guess here).

In a debug build, ASSERT and VERIFY do exactly the same thing. It is also
true that in a retail build, neither ASSERT nor VERIFY display the
notification window when the condition being tested if false. So what's the
difference between the two? The difference is what happens with VERIFY's
arguments in a retail build. With ASSERT, nothing happens because the
preprocessor completely strips out the ASSERT statement; however, with
VERIFY the expression that VERIFY is testing will be executed. For
example, assume you have a function called sum that adds two global
variables together and puts their sum in another global variable and
returns TRUE if the sum was positive.

int a = 3;
int b = -4
int c = 1;

ASSERT( sum() )
printf( "%d", c);

In a retail build, the preprocessor completely strips out the ASSERT
statement so sum will not be called and c will equal 1 in the printf call.


Now let's look at the same code using VERIFY.

int a = 3;
int b = -4
int c = 1;

VERIFY( sum() )
printf( "%d", c);

In a retail build, the expression that is passed as the argument to VERIFY
** is ** executed, so sum is called and c equals -1 when the printf
statement is executed. However, even though foo evaluates to a FALSE, the
notification window will not be displayed. You lose out on the
notification, but at least the code still executed.

Not understanding the difference between ASSERT and VERIFY is a common
cause of bugs that only appear in the retail build of an executable. The
bug is a result of a an ASSERT expression not being executed in the retail
version. So you should use VERIFY instead of ASSERT, if you are going to
test an expression that needs to be executed in both the debug and retail
versions of your executable.

I hope this explanation helps.
Al Margheim


Thomas Bonnes

unread,
Mar 31, 1997, 3:00:00 AM3/31/97
to

Chris Marriott <ch...@chrism.demon.co.uk> wrote:

>In article <5he7ov$p...@topgun.es.dupont.com>, Thomas Bonnes <bonnestj@a1
>.esvax.umc.dupont.com> writes

>> I know this is a very simple topic, but can someone tell me more of
>>what assertions actually are? I see many examples in the good number
>>of books that I have, and even a few lame attempts to explain to me
>>what they exactly are used for( for example ::: when, why, how,
>>etc...... do you use ASSERT(something) statements ). Do assertions
>>just stop a function execution line path of code or do they do much
>>more than that. And where does using them gain you the most value in
>>programs?

>"ASSERT" is used to catch bugs in a debug build. Its argument is an


>expression which must evaluate to TRUE. If any other result occurs, the
>program will be terminated.


Chris, Al, etc...


Thanks to all both on here and by private email ! I had somewhat of
an idea before hand what was going on, but this helps to clear up my
thinking a whole lot. Usenet is a very valuable resource once again.

Tom Bonnes
bon...@ssnet.com


0 new messages