Pointers are an important concept in C++ that makes the language stand out against other languages. To dynamically access any memory location via pointers enables developers to design low-level programs.
However, developers typically face the dangling pointer problem, where a pointer points to a memory location that has been deleted or freed.
Below are multiple scenarios where the dangling pointer problem may occur.
delete
keyword#include <iostream>using namespace std;int main() {//memory allocationint *ptr = new int(2);cout<<*ptr<<endl;//dangling pointerdelete ptr;cout<<*ptr;}
The ptr
pointer keeps on pointing to the deleted location. This makes the ptr pointer act as a dangling pointer.
free()
function#include <iostream>using namespace std;int main() {//memory allocationint *ptr = new int(2);cout<<*ptr<<endl;//dangling pointerfree(ptr);cout<<*ptr;}
The ptr
pointer keeps on pointing to the memory location that has been freed using the free()
function. The ptr
pointer acts like a dangling pointer as it has no available memory location to point towards.
#include <iostream>using namespace std;int* ptrLocation(){//local variable xint x = 20;//returning local variable's addressreturn &x;}int main() {//assigning local variable x's address to ptrint* ptr = ptrLocation();//no value as x was deleted as soon as the//execution of ptrLocation() function is finishedcout<<*ptr;}
In the above code snippet, it seems like the ptr
pointer is getting the address of the local variable x
, but the life of variable x
is the same as the ptrLocation()
function. As soon as its execution is finished, the local variable x
is also deleted. This leaves ptr
pointing to a location that has been deleted and makes it a dangling pointer.
#include <iostream>using namespace std;int main() {int* ptr = new int(5);{int x = 10;ptr = &x;}cout<<*ptr;}
The value that ptr
previously held was 5. Then inside another scope, it kept the value of 10. This makes the ptr
pointer inside the main scope a dangling pointer pointing to a memory location that is out of its scope.
To avoid the dangling pointer problem, we can set the pointer to nullptr
after the memory location has been deleted or freed:
#include <iostream>using namespace std;int main() {//memory allocationint *ptr = new int(2);int *ptr2 = new int(5);//dangling pointerdelete ptr;ptr = nullptr;//dangling pointer 2free(ptr2);ptr2 = nullptr;cout<<*ptr;cout<<*ptr2;}
As seen from the output, now, if we try to print the ptr
pointer's deleted value, it will throw an error. Logically, it is correct as the ptr
pointer is pointing towards nullptr
.
For functions with local variables and scope changes, we can use the static
keyword to avoid this condition
#include <iostream>using namespace std;int* ptrLocation(){//local variable xstatic int x = 20;//returning local variable's addressreturn &x;}int main() {//assigning local variable x's address to ptrint* ptr = ptrLocation();//no value as x was deleted as soon as the//execution of ptrLocation() function is finishedcout<<*ptr<<endl;int* ptr2;{static int y = 10;ptr2 = &y;}cout<<*ptr2<<endl;}
The scope of the local variable x
has been changed, and it is now accessible by ptr
.
The variable y
is also declared using the static
keyword, and as its scope is global, it avoids making ptr2
a dangling pointer.
The dangling pointer problem is a common logical error that developers face. This condition must be avoided in C++ as it's difficult to debug.
Free Resources