Re: assert

From: Carl Daniel [VC++ MVP] (cpdaniel_remove_this_and_nospam_at_mvps.org.nospam)
Date: 04/14/04


Date: Tue, 13 Apr 2004 20:31:53 -0700

bdg wrote:
> MSDN says that "The assert routine is available in both the release
> and debug versions of the C run-time libraries."
>
> How can it be true that "The assert routine is available in both the
> release and debug versions of the C run-time libraries"? My testing
> shows that assert() statements are only executed when compiling
> project into Debug version.

It is kind of silly. By "available" they mean that the program will compile
and link with either RTL. Of course, assert() doesn't do anything in a
release build.

>
> Why does _DEBUG has to be defined since if building project into Debug
> version, no need to define _Debug any more?

Unfortunately, there are two macros:
    NDEBUG is defined in release builds (mandated by the C and C++
standards)
    _DEBUG is defined in debug builds (VC++ convention).
>
> "Two other assertion macros, _ASSERT and _ASSERTE, are also
> available, but they only evaluate the expressions passed to them when
> the _DEBUG flag has been defined."
>
> When to use _ASSERT or _ASSERTE, instead of assert?

Personal preference, mostly. assert() evaluates only when NDEBUG is not
defined (kinda backwards, eh?), while these others evaluate only when _DEBUG
is defined. In the end, they all do about the same thing. Personally, I'd
stick to assert() since it's defined by the C and C++ standards and is
therefore portable to other systems.

-cd



Relevant Pages

  • Re: Weird error during programme startup
    ... Microsoft Visual C++ Debug Library ... I've stepped through the innards of _initterm and it ... it won't assert in release builds. ... You said that you moved all strings to string tables. ...
    (microsoft.public.vc.language)
  • Re: Tips to hide "Debug Assertion Failed!"
    ... Assert failures only come from the debug version. ... and also represents a bug in your program. ... Joseph M. Newcomer [MVP] ...
    (microsoft.public.vc.mfc)
  • Using debug print routine inside assert
    ... The idea here is to have consistent debug print messages. ... If you could skip the evaluation of debug, ... return True # return true so that assert always succeeds ... nevertheless, in Python, you don't have such thing as macros. ...
    (comp.lang.python)
  • Re: Assertion in DEBUG and RELEASE
    ... >> code is like this, I was getting a pointer, and check with an ASSERT ... Those bugs should be fixed for a release build, ... The debug CRT initializes that memory with NULL, ... Also notable: compilers ...
    (microsoft.public.vc.language)
  • Re: behaviour of vector::operator[]
    ... > programming. ... assert to check the precondition. ... Moreover for debug purposes, you ... checks in certain places using some sort of asserts, not exceptions. ...
    (microsoft.public.vc.stl)