Поделиться через


CRT Assertions

This topic applies to:

Edition

Visual Basic

C#

F#

C++

Web Developer

Express

Topic does not apply Topic does not apply Topic does not apply

Native only

Topic does not apply

Pro, Premium, and Ultimate

Topic does not apply Topic does not apply Topic does not apply

Native only

Topic does not apply

The CRTDBG.H header file defines the _ASSERT and _ASSERTE macros for assertion checking.

Macro

Result

_ASSERT

If the specified expression evaluates to FALSE, the file name and line number of the _ASSERT.

_ASSERTE

Same as _ASSERT, plus a string representation of the expression that was asserted.

_ASSERTE is more powerful because it reports the asserted expression that turned out to be FALSE. This may be enough to identify the problem without referring to the source code. However, the Debug version of your application will contain a string constant for each expression asserted using _ASSERTE. If you use many _ASSERTE macros, these string expressions take up a significant amount of memory. If that proves to be a problem, use _ASSERT to save memory.

When _DEBUG is defined, the _ASSERTE macro is defined as follows:

#define _ASSERTE(expr) \
   do { \
      if (!(expr) && (1 == _CrtDbgReport( \
         _CRT_ASSERT, __FILE__, __LINE__, #expr))) \
         _CrtDbgBreak(); \
   } while (0)

If the asserted expression evaluates to FALSE, _CrtDbgReport is called to report the assertion failure (using a message dialog box by default). If you choose Retry in the message dialog box, _CrtDbgReport returns 1 and _CrtDbgBreak calls the debugger through DebugBreak.

Replacing printf

_ASSERTE allows you to replace the following code:

#ifdef _DEBUG
   if ( someVar > MAX_SOMEVAR )
      printf( "OVERFLOW! In NameOfThisFunc( ),
               someVar=%d, otherVar=%d.\n",
               someVar, otherVar );
#endif

with a single statement:

_ASSERTE(someVar <= MAX_SOMEVAR);

Checking for Heap Corruption

The following example uses _CrtCheckMemory to check for corruption of the heap:

_ASSERTE(_CrtCheckMemory());

Checking Pointer Validity

The following example uses _CrtIsValidPointer to verify that a given memory range is valid for reading or writing.

_ASSERTE(_CrtIsValidPointer( address, size, TRUE );

The following example uses _CrtIsValidHeapPointer to verify a pointer points to memory in the local heap (the heap created and managed by this instance of the C run-time library — a DLL can have its own instance of the library, and therefore its own heap, outside of the application heap). This assertion catches not only null or out-of-bounds addresses, but also pointers to static variables, stack variables, and any other nonlocal memory.

_ASSERTE(_CrtIsValidPointer( myData );

Checking a Memory Block

The following example uses _CrtIsMemoryBlock to verify that a memory block is in the local heap and has a valid block type.

_ASSERTE(_CrtIsMemoryBlock (myData, size, &requestNumber, &filename, &linenumber));

See Also

Concepts

Assertions