Dangling Pointer in programming
Last Updated :
14 May, 2024
In programming especially in C and C++, pointers play a very important role in managing memory and improving performance. However incorrect use of pointers can lead to issues. One such issue is creation of dangling pointers. In this article, we will explore what dangling pointers are how they occur and how to prevent them.

What is Dangling Pointer in Programming?
Dangling Pointer in programming refers to a pointer that doesn’t point to a valid memory location. This usually happens when an object is deleted or deallocated, without modifying the value of the pointer, so it still points to the memory location of the deallocated memory.
Example:
Dangling Pointer Example
int* ptr = new int(5);
delete ptr;
// ptr is now a dangling pointer
Dangling Pointer in C:
The below example demonstrates a simple program that creates a dangling pointer in C.
Example 1
#include <stdio.h>
int main()
{
// Allocating memory
int* ptr = malloc(5 * sizeof(int));
// Deallocating memory
free(ptr);
// ptr is now a dangling pointer
printf("ptr is now a dangling pointer");
return 0;
}
Example 2
#include <stdio.h>
int* ptr;
void gfgFnc()
{
// Allocating memory
int arr[] = { 1, 2, 3, 4, 5 };
ptr = arr;
// Memory gets deallocated automatically outside the
// function
}
int main()
{
gfgFnc();
// ptr is now a dangling pointer
printf("ptr is now a dangling pointer\n");
for (int i = 0; i < 5; i++)
printf("%d ", *(ptr + i));
return 0;
}
Outputptr is now a dangling pointer
Dangling Pointer in C++:
The below example demonstrates a simple program that creates a dangling pointer in C++.
Example 1
// C++ program demonstrating a dangling pointer
#include <iostream>
using namespace std;
int main()
{
// Allocating memory
int* ptr = new int(5);
// Deallocating memory
delete ptr;
// ptr is now a dangling pointer
cout << "ptr is now a dangling pointer" << endl;
return 0;
}
Example 2
#include <iostream>
using namespace std;
int* ptr;
void gfgFnc()
{
// Allocating memory
int arr[] = { 1, 2, 3, 4, 5 };
ptr = arr;
// Memory gets deallocated automatically outside the
// function
}
int main()
{
gfgFnc();
// ptr is now a dangling pointer
cout << "ptr is now a dangling pointer\n";
for (int i = 0; i < 5; i++)
cout << *(ptr + i) << " ";
return 0;
}
Outputptr is now a dangling pointer
Why is it important to handle Dangling Pointers?
Handling dangling pointers is important for maintaining the integrity and stability of a program. Dangling pointers occur when a pointer points to a memory location that has been deallocated or freed. If a program attempts to access or modify the memory through a dangling pointer, it can lead to unpredictable behavior, including crashes and data corruption.
By handling dangling pointers properly, we can improve the reliability and security of the program and prevent any unpredictable access to any memory location.
Preventing Dangling Pointers:
To prevent dangling pointers, always set pointers to NULL or nullptr after you have finished using them and have deallocated the memory they point to. This ensures that the pointers do not point to deallocated memory and become dangling pointers.
Similar Reads
Error Handling in Programming In Programming, errors occur. Our code needs to be prepared for situations when unexpected input from users occurs, division by zero occurs, or a variable name is written incorrectly. To prevent unexpected events from crashing or having unexpected outcomes, error handling involves putting in place m
12 min read
Nested Loops in Programming In programming, Nested Loops occur when one loop is placed inside another. These loops are quite useful in day-to-day programming to iterate over complex data structures with more than one dimension, such as a list of lists or a grid. In this article, we will learn about the basics of nested loops a
6 min read
Functions in Programming Functions in programming are modular units of code designed to perform specific tasks. They encapsulate a set of instructions, allowing for code reuse and organization. In this article, we will discuss about basics of function, its importance different types of functions, etc.Functions in Programmin
14 min read
Loops in Programming Loops or Iteration Statements in Programming are helpful when we need a specific task in repetition. They're essential as they reduce hours of work to seconds. In this article, we will explore the basics of loops, with the different types and best practices. Loops in ProgrammingTable of Content What
12 min read
What is a Block in Programming? In programming, a block is a set of statements that are grouped and treated as a single unit. Blocks are used to define the scope of variables, control the flow of execution in conditional statements and loops, and encapsulate code in functions, methods, or classes. Table of Content What is a Block
6 min read
What are Operators in Programming? Operators in programming are essential symbols that perform operations on variables and values, enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types. Operators in Programming Table of
15+ min read