The goal of this exercise is to understand what are the main issues when dealing with owning raw pointers and how tools like an Address sanitizer can help identify them.
Exercise: memory sanitizers
The compiler can instrument the code so that at run-time a number of checks can be performed and, in case an anomaly is detected, a diagnostic can be issued.
Here we introduce just the address sanitizer.
$ g++ -g -Wall -Wextra -o memerrors memerrors.cpp -fsanitize=address
$ ./memerrors
...
Note how the compiler can sometimes warn us about problems in our code.
Exercise: memory issues
Build and run, directly and through the address sanitizer, the following small programs:
non_owning_pointer.cpp. Uncomment the lines containing calls tofreeordeleteand see what the consequences are at run-timearray_too_small.cppleak.cpp. Comment the allocations and see what the consequences are at run-timedouble_delete.cppmissed_delete.cpp. Uncomment the lines about thetry/catchblock and position them in different places; see what the consequences are at run-time
Try and fix the problems.
Exercise: fix issues with resource-managing types
Adapt non_owning_pointer.cpp, array_too_small.cpp, leak.cpp,
double_delete.cpp and missed_delete.cpp to use resource-managing types, such as
smart pointers, to fix the memory issues.