3 Pointers

A pointer in C or C++ is a very primitive construct. A pointer is simply a variable that contains the address of something. In fact, the concept of a pointer traces all the way down to assembly level instructions. An “indirect” operand in assembly language programming is the origin of a pointer.

Because pointers are such low-level constructs, they provide certain benefits, such as code size efficiency as well as run-time efficiency. At the same time, because pointers are low-level constructs, any misuse can also lead to disastrous results.

There are three main categories of disasters.

Combined with dynamic memory allocation, the use of low-level pointers can lead to memory leak. In other words, the only pointer pointing to a dynamically allocated memory object can be overwritten. Once this pointer is overwritten, the dynamically allocated object is no longer accessible. This also means that the object can no longer be deallocated.

The second category of disaster can be detected fairly easily. A program may attempt to delete the same object twice. This can be detected because the library subroutines can check if a chunk of memory is already deallocated.

The third category of disaster is just memory corruption. Because pointers can be modified in many ways (in C and in C++), they can be modified so that they point to memory locations that should be out of reach. Low-level pointers do not understand object boundaries, and therefore it is impossible to even check for memory corruption problems.