Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.36/5 (4 votes)
See more:
What is Dangling pointer in c++?
Posted
Comments
[no name] 6-Aug-14 6:19am    
That's a great question to practice your research skills on.

I wonder if there is a dedicated page on Wikipedia[^].
 
Share this answer
 
A dangling pointer is a pointer that points to the unavailable memory location or data which is not valid,
for eg:

C#
#include<iostream.h>
void main(){
int *ptr1;
int *ptr2 = new int;
ptr1=ptr2;
delete ptr2;
}


In the above example, ptr1 and ptr2 are pointing to the same location in memory.
but after deletion of ptr2. Ptr1 can not able to point any memory.
This is called as dangling pointer.
 
Share this answer
 
C#
int *ptr = (int *)malloc(sizeof(int));
.............
.............
free(ptr);

// ptr is a dangling pointer now and operations like following are invalid
*ptr = 10;  // or printf("%d", *ptr);
// EXAMPLE 2
int *ptr = NULL
{
   int x  = 10;
   ptr = &x;
}
// x goes out of scope and memory allocated to x is free now.
// So ptr is a dangling pointer now.
 
Share this answer
 
Comments
CPallini 6-Aug-14 6:26am    
5.
Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. These are special cases of memory safety violations. More generally, dangling references and wild references are references that do not resolve to a valid destination, and include such phenomena as link rot on the internet.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900