Dynamic Memory
By using dynamic memory the following limitations of static memory are completely eliminated.
- Size of datatypes and arrays have to be specified once and cannot be changed at runtime.
- Changing the sizes requires a recompilation of the whole project.
Overview of Problems with Dynamic Memory
Because the dynamic memory approach most operations are transferred to run time and can therefore not be verified by the compiler. To master the handling of dynamic memory different libraries were developed to support the programmer to supplement the compiler, as it can only detect problems and errors at compile time.
In the following we present an overview of problems often encountered when dealing with dynamic memory allocation. The arrangement is closely related to the problems discussed in the lecture Programmieren 2. The corresponding source files can be found under the menu entry: Test Programs
- Error 01: access without allocation
- Error 02: access before allocation
- Error 03: no validation of allocation
- Error 04: wrong size for memory allocation
- Error 05: access of uninitialized memory
- Error 06: memory leaks, memory without reference
- Error 07: access to already freeed memory
- Error 08: free and non-valid pointer
The following programs and libraries are introduced which support the programmer:
- DUMA needs a recompilation or a relink step of the tested program. We use this program in our lectures due to its ease of use. The drawback of this program is that it cannot detect all the problems, as can be seen in the following table.
- valgrind is clearly one of the most important test programs. valgrind requires no recompilation of the program, so it's very easy to set up. It identified almost all of the incorrect pointer uses and memory leaks. The drawback of this program is that the usage needs more skills than the DUMA library.
The features of these programms to detect errors in our test programs are summarized next:
| Checker | Error 01 | Error 02 | Error 03 | Error 04 | Error 05 | Error 06.1 | Error 06.2 | Error 07 | Error 08 |
|---|---|---|---|---|---|---|---|---|---|
| DUMA | missing | missing | missing | OK | missing | OK | OK | missing | OK |
| valgrind | OK | OK | OK | OK | can't | OK | OK | OK | OK |
This page was inspired by http://www.cs.utexas.edu/users/jpmartin/memCheckers.html