By that I mean, what do I need to do to have useful assertions in my code?
MFC is quite easy, i just use ASSERT(something).
What's the non-MFC way?
Edit: Is it possible to stop assert breaking in assert.c rather than than my file which called assert()?
Edit: What's the difference between <assert.h>
& <cassert>
?
Accepted Answer: Loads of great answers in this post, I wish I could accept more than one answer (or someone would combine them all). So answer gets awarded to Ferruccio (for first answer).
The C language provides an <assert. h> header file and corresponding assert() macro that a programmer can use to make assertions. If an assertion fails, the assert() macro arranges to print a diagnostic message describing the condition that should have been true but was not, and then it kills the program.
You should only use assert to check for situations that "can't happen", e.g. that violate the invariants or postconditions of an algorithm, but probably not for input validation (certainly not in libraries). When detecting invalid input from clients, be friendly and return an error code.
To properly use assertions as a debugging tool, you shouldn't use try … except blocks that catch and handle AssertionError exceptions. If an assertion fails, then your program should crash because a condition that was supposed to be true became false.
#include <cassert> assert(something);
and for compile-time checking, Boost's static asserts are pretty useful:
#include <boost/static_assert.hpp> BOOST_STATIC_ASSERT(sizeof(int) == 4); // compile fails if ints aren't 32-bit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With