-
Book Overview & Buying
-
Table Of Contents
C++ Memory Management
By :
Assertions are statements of fact that programmers think should be upheld by code. Some are dynamic, based on information known at runtime, for example, “The following pointer should not be null at this point.” Others are static, based on information known at compile time, for example, “This program has been written with the non-portable assumption that an int occupies four bytes of storage.” In the latter case, we have a program that has been written based on a non-portable assumption and we have to live with this choice, but we do not want our code to compile on platforms where that assumption does not hold.
For dynamic assertions, it is customary to use the assert() macro from the <cassert> header. That macro takes as argument a boolean expression and halts program execution if it evaluates to false:
void f(int *p) {
assert(p); // we hold p != nullptr to be true
// use *p
} Note that many projects...