You need to understand that memory leaks do not concern pointers (in fact: never - even if many people will require something else). The entire business with pointers is simply misleading.
They are associated with a mismatch in the allocation and release of dynamic memory.
Each selection through new must match one selection through delete . The same goes for malloc and free and new[] and delete[] (and other proposed dynamic resource allocation functions).
int* x; // Not a memory leak: no dynamic allocation new int; // Memory leak: we acquired a value via `new` and lost it. int* y = new int; int* z = y; delete y; // Not a memory leak any more: we freed the memory. delete z; // Oooh, bad: we deleted a value twice. The horror.
Modern C ++ code uses very little (in most cases: no ) manual allocation of dynamic memory. This way you cannot have leaks. Basically. This is very good, so do it. Instead of manually allocating dynamic memory, you can use standard containers and smart pointers that handle memory management for you.
source share