Seven Dynamic Variable Sins
The next seven exercises illustrate seven ways that misusing dynamic variables can destroy your program, either by sending it to the chaos of heap corruption or invoking the sudden thunderbolt of an operating system trap.
Several of the following exercises were contrived to print error messages and terminate the program. The specific message produced depends both on the C++ runtime system version, and on the operating system on which the program runs. There is no guarantee you will see the same error message, so each example also contains a description of what happens.
Exercise 41: Using a Dynamic Variable before Creating It
The first deadly dynamic variable sin is using a pointer to a dynamic variable before creating the dynamic variable. It should be obvious that dereferencing a pointer to invalid storage will cause undefined behavior, which may include a crash, or simply producing the wrong result.
Note
The complete code for the exercise...