Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. C/C++ Assertions
In this articleApplies to: Visual Studio Visual Studio for Mac Visual Studio CodeAn assertion statement specifies a condition that you expect to be true at a point in your program. If that condition is not true, the assertion fails, execution of your program is interrupted, and the Assertion Failed dialog box appears. Visual Studio supports C++ assertion statements that are based on the following constructs:
In this topicHow assertions work Assertions in Debug and Release builds Side effects of using assertions CRT assertions MFC assertions
How assertions workWhen the debugger halts because of an MFC or C run-time library assertion, then if the source is available, the debugger navigates to the point in the source file where the assertion occurred. The assertion message appears in both the Output window and the Assertion Failed dialog box. You can copy the assertion message from the Output window to a text window if you want to save it for future reference. The Output window may contain other error messages as well. Examine these messages carefully, because they provide clues to the cause of the assertion failure. Use assertions to detect errors during development. As a rule, use one assertion for each assumption. For example, if you assume that an argument is not NULL, use an assertion to test that assumption. In this topic Assertions in Debug and Release buildsAssertion statements compile only if Side effects of using assertionsWhen you add assertions to your code, make sure the assertions do not have side effects. For example, consider the
following assertion that modifies the
Because the Be especially careful about using function calls in assertion statements, because evaluating a function can have unexpected side effects.
In this topic CRT assertionsThe CRTDBG.H header file defines the _ASSERT and _ASSERTE macros for assertion checking.
When
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, If you need to temporarily disable all assertions, use _CtrSetReportMode. Checking for Heap CorruptionThe following example uses _CrtCheckMemory to check for corruption of the heap:
Checking Pointer ValidityThe following example uses _CrtIsValidPointer to verify that a given memory range is valid for reading or writing.
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.
Checking a Memory BlockThe following example uses _CrtIsMemoryBlock to verify that a memory block is in the local heap and has a valid block type.
In this topic MFC assertionsMFC defines the ASSERT macro for assertion checking. It also defines the If the argument of the MFC When an assertion fails, a message dialog box shows the name of the source file and the line number of the assertion. If you choose Retry in the dialog box, a call to AfxDebugBreak causes execution to break to the debugger. At that point, you can examine the call stack and use other debugger facilities to determine why the assertion failed. If you have enabled Just-in-time debugging, and the debugger was not already running, the dialog box can launch the debugger. The following example shows how to use
You can use ASSERT with the IsKindOf function to provide type checking of function arguments:
The MFC ASSERT_VALID and CObject::AssertValidThe CObject::AssertValid method provides run-time checks of the internal state of an object. Although you are not required to override The following example shows how to declare an
When you override
If any of
your member variables store objects, you can use the For example, consider a class
The
Validity testing does not stop at this level because the class With some more work, you could add validity tests for the This is a powerful mechanism when you build for debugging. When you subsequently build for release, the mechanism is turned off automatically. Limitations of AssertValidA triggered assertion indicates that the object is definitely bad and execution will stop. However, a lack of assertion indicates only that no problem was found, but the object is not guaranteed to be good. In this topic Using assertionsCatching logic errorsYou can set an assertion on a condition that must be true according to the logic of your program. The assertion has no effect unless a logic error occurs. For example, suppose you are simulating gas molecules
in a container, and the variable
Or you might include a CRT assertion like this:
These statements do nothing if your program is operating correctly. If a logic error causes In this topic Checking resultsAssertions are valuable for testing operations whose results are not obvious from a quick visual inspection. For example, consider the following code, which updates the variable
The number of molecules counted by In this topic Finding unhandled errorsYou can use assertions to test for error conditions at a point in your code where any errors should have been handled. In the following example, a graphic routine returns an error code or zero for success.
If the error-handling code works properly,
the error should be handled and Assertion statements are not a substitute for error-handling code, however. The following example shows an assertion statement that can lead to problems in the final release code:
This code relies on the assertion statement to handle the error condition. As a result, any error code returned by In this topic See also
FeedbackSubmit and view feedback for Are written predictions that specify relations between variables?Analyses are written predictions that specify relationships between variables. A correlation describes the statistical relationship between two variables. The strength of a correlation can be inferred from the "compactness" of its scatterplot.
Is an example of group mechanisms?Group mechanisms include Team Characteristics and Diversity, Team Processes and Communication, Leader Power and Negotiation, Leader Styles and Behaviors.
Which of these takes all the correlations found in studies of a relationship and calculates a weighted average of them?Meta-analysis takes all of the correlations found in studies of a particular relationship and calculates a weighted average (such that correlations based on studies with large samples are weighted more than correlations based on studies with small samples).
Which of the following is a primary individual outcome of interest to organizational behavior researchers according to the integrative model?Job performance is the primary individual outcome of interest to OB researchers according to the model.
|