What is the dangling pointer problem in C++?

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.

A pointer pointing to a deleted or freed location

Examples

Below are multiple scenarios where the dangling pointer problem may occur.

Using the delete keyword

#include <iostream>
using namespace std;
int main() {
//memory allocation
int *ptr = new int(2);
cout<<*ptr<<endl;
//dangling pointer
delete ptr;
cout<<*ptr;
}

The ptr pointer keeps on pointing to the deleted location. This makes the ptr pointer act as a dangling pointer.

Using the free() function

#include <iostream>
using namespace std;
int main() {
//memory allocation
int *ptr = new int(2);
cout<<*ptr<<endl;
//dangling pointer
free(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.

Pointing to a local variable

#include <iostream>
using namespace std;
int* ptrLocation(){
//local variable x
int x = 20;
//returning local variable's address
return &x;
}
int main() {
//assigning local variable x's address to ptr
int* ptr = ptrLocation();
//no value as x was deleted as soon as the
//execution of ptrLocation() function is finished
cout<<*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.

Change of scope

#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.

Solutions

  • 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 allocation
int *ptr = new int(2);
int *ptr2 = new int(5);
//dangling pointer
delete ptr;
ptr = nullptr;
//dangling pointer 2
free(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.

Static keyword

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 x
static int x = 20;
//returning local variable's address
return &x;
}
int main() {
//assigning local variable x's address to ptr
int* ptr = ptrLocation();
//no value as x was deleted as soon as the
//execution of ptrLocation() function is finished
cout<<*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.

Conclusion

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

Copyright ©2025 Educative, Inc. All rights reserved