Memory issues

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++ -fsanitize=address -g -Wall -Wextra -o memerrors memerrors.cpp
$ ./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:

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 to use resource-managing types, such as smart pointers, to fix the memory issues.