|[27 earlier articles]|
|Re: failure due to compiler? email@example.com (1996-07-22)|
|Re: failure due to compiler? firstname.lastname@example.org (Lee Webber) (1996-07-23)|
|Re: failure due to compiler? email@example.com (Eric Hamilton) (1996-07-23)|
|Re: failure due to compiler? firstname.lastname@example.org (1996-07-23)|
|Re: failure due to compiler? email@example.com (1996-07-24)|
|Re: failure due to compiler? firstname.lastname@example.org (1996-07-26)|
|Re: failure due to compiler? email@example.com (1996-07-31)|
|Re: failure due to compiler? firstname.lastname@example.org (1996-07-31)|
|From:||email@example.com (Dave Gillett)|
|Date:||31 Jul 1996 19:26:19 -0400|
|References:||96-07-041 96-07-123 96-07-141 96-07-173 96-07-180|
)>However, as expressions in C++ (and Eiffel) may have side effects, turning
)>assertion checking off may alter the behaviour of the program. One Eiffel
)>user I know claims to have experienced this perplexing behaviour. His
)>program worked as expected until he turned assertion checking off!
Dave Gillett <firstname.lastname@example.org> wrote:
) The particular home-grown flavour of assertion-checking macro that I like
)has the "feature" of deliberately evaluating the tested expression *twice*.
)Code that relies on side-effects of such evaluation typically breaks while
)I'm still building debug versions.
>Yes, that is a common flaw in <assert.h> implementations. DEC shipped
>such a defective <assert.h> until I complained. Here is one which
>I believe works. I typed it in just now, from a file which does work.
>But I may have made a little typo. The __VAL__ __STR__ thing is a
>standard trick used with the preprocessor to evaluate strings. Note that
>the predicate is evaluated exactly once.
IT IS NOT A FLAW, IT IS A FEATURE.
Since assertion predicates will not be evaluated in release builds, code
which relies on side-effects of assertion predicate evaluation will be broken.
Predicates without side-effects avoid any possibility of this happening.
By evealuating the predicate twice, the implementor of assert() tries to
ensure that predicates with side-effects will break in the debug build. This
is preferable to a program which works fine in the debug build, but fails
mysteriously in the release build.
(Recall that efficiency of debug builds is of even less importance than it is
of release builds. In my view, even in release builds, efficiency is less
important than correct program behaviour, and an implementation of assert() is
only as valuable as the reasons it provides for believing that the release
build will function correctly.)
I didn't further explain that this is my own home-grown version of assert(),
with a couple of other things I like, too. Like a dialog box that gives me the
choice of aborting the task, popping into the debugger, or ignoring the failure
and continuing. Like a descriptive message instead of a naked/cryptic
predicate. Like a complementary form for those cases where it's easier to
construct a predicate that should be FALSE than one that should be TRUE.
[I agree, it's a feature. Clever hack. For the truly paranoid, perhaps
you could make the number of times it evaluates the condition settable at
compile time. -John]
Return to the
Search the comp.compilers archives again.