🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1652

Re: ANSI assert

#1652
From: Mike Coleman
Date: Sat, 08 Sep 1990 16:34
40 lines
2102 bytes
joe@proto.COM (Joe Huffman) writes:
>My concern is when NDEBUG is defined.  I have many places in my code where I
>do something like the following:
>  assert(tree_init() == OKAY);

>My definition of assert with debugging turned off looks like:

>#define assert(test) ((void)(test))

>Am I in error for doing this? 

Yes, at least the way I understand things.  Nothing which is necessary for the
correctness of your program should be put in an assert.  Only expressions
necessary to *test* the assertion should be in the assert.  This is an easy
mistake to make; I still do it occasionally.  So, the correct version of the
code you give above (as I read it) would be

	result = tree_init();
	assert(result == OKAY);

The point of having the assert in the first place is that you can put checks
for correctness in your program which are extremely expensive, and then remove
them simply by defining NDEBUG.  For example, imagine this piece of code in a
malloc implementation:

	assert(validate_heap());  /* an expensive function which validates the
					heap to within an inch of its life. */

The version of assert you assume does not give the desired effect here.  In
particular, casting an expression to 'void' does *not* allow the compiler to
throw it away, in and of itself.

In any case, if you want this behavior, just define your own macro.  But
please, don't call it 'assert'.

Hope this helps.
-- 
Nothing's ever late when it's measured in Programmer's Time: ++ coleman
  |     |     |     |     |     |     |     |     |          ++ @twinsun.com
BEGIN  1/2   2/3   3/4   4/5   5/6   6/7   7/8   8/9  (etc)  ++ @cs.ucla.edu
Technical Details
Message-ID: <1990Sep8.163442.5346@twinsun.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!ucsd!usc!ucla-cs!twinsun!coleman
References: <1428@proto.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!ucsd!usc!ucla-cs!twinsun!coleman
From: coleman@twinsun.com (Mike Coleman)
Newsgroups: comp.std.c
Subject: Re: ANSI assert
Keywords: assert, NDEBUG
Message-ID: <1990Sep8.163442.5346@twinsun.com>
Date: 8 Sep 90 16:34:42 GMT
References: <1428@proto.COM>
Sender: news@twinsun.com
Organization: Twin Sun, Inc
Lines: 40
Nntp-Posting-Host: farside