C.1
Using Assert



C++ provides a convenient macro assert, that displays an informative message and aborts a program if the expression passed is 0. The example below uses assert to determine if a dynamic memory allocation error occurred instead of the if statement shown in 16.2.2 Checking for Allocation Errors. If memory can not be allocated, head_ptr is set to 0 and the program is terminated.

head_ptr = new country_node;
assert(head_ptr);

This time the program will be aborted if memory can not be allocated.