Memory Access Summary
Here is a list of the most important points that we have raised about memory access.
- The operators new and delete give the C++ programmer increased power as well as increased responsibility.
- Improper use of pointers and dynamic memory can cause program crashes that result from heap corruption and memory leaks.
- Qt and STL container classes permit the safe use of dynamic memory without adversely affecting performance.
- In a multiple variable declaration, the unary * operator applies only to the variable that immediately follows it, not the type that precedes it.
- Dereferencing an uninitialized pointer is a serious error that may not be caught by the compiler.
- After delete has been applied to a pointer, the state of that pointer is undefined.
- It is good practice to assign NULL to a pointer immediately after it has been deleted.
- Applying delete to a non-null pointer that was not returned by new produces undefined results.
- The compiler cannot be relied upon to detect the improper use of delete, so it is the programmer's responsibility to use delete correctly.
- A memory leak is produced when a program causes memory to be allocated and then loses track of that memory so that it can neither be accessed nor deleted.