Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do pointers cause memory leak to dynamically allocated integer?

“Because pnValue is the only variable holding the address of the dynamically allocated integer, when pnValue is destroyed there are no more references to the dynamically allocated memory. This is called a memory leak. As a result, the dynamically allocated integer can not be deleted, and thus can not be reallocated or reused.” {quoted from LearnCpp.}

Why does pnValue holds the address of the dynamically allocated integer? If pointers just point to the address, why should destroying the pointer affect the address? Does this mean that in dynamically allocated integers, we must always have pointers because the pointer somehow is the address?


2 Answers

All objects that have been defined exist in memory and has an address. An address of the object is the location of the memory from where, the object starts residing. So in a one dimensional memory space, if an object starts residing at the slot 100, then 100 is said to be the address of the object.

 99 100 101  102  103  104  105  106
=====================================
   | O  | B  | J  | E  | C  | T  |    
=====================================
     ^
     |
     |
 pnObject

Pointers are objects which contains addresses of other objects. When you create an object dynamically, it is created in the heap memory, and has no scope. It continues to exist as long as you don;t delete the allocated memory explicitly or the program allocating the memory dies.

Memory is allocated dynamically through the c-library function malloc or the c++ operator new, which returns the address, the starting location in the heap memory from where the object starts residing. You are supposed to save the address in a variable ( a particular type called a pointer), which now holds the address of the object.

Object *pnValue = new Object();

As long as you know the address of the newly created object in heap, you have control but if you forget the address, either by destroying the pointer variable, overlaying the content or simply not storing the address, you cannot access that location and going forward cannot free/delete the allocated memory.

 99 100 101  102  103  104  105  106
=====================================
   | O  | B  | J  | E  | C  | T  |    
=====================================
like image 187
Abhijit Avatar answered Feb 02 '26 08:02

Abhijit


Pointers "just point to the address". What this is saying is, if you remove all pointers which point to that address, there's no way to "reach" the memory stored there in order to delete/free it. As soon as no pointer "points" to that location, there's no way for you to clean up that memory any more, so you get a leak.

Destroying the pointer does not create the leak - if there is another pointer which you can use to reach the memory in order to clean it up. Destroying the pointer just removes the ability to reach that memory if no other pointers are kept.

like image 26
Reed Copsey Avatar answered Feb 02 '26 07:02

Reed Copsey