You are on page 1of 4

A dangling pointer points to memory that has already been freed. The storage is no longer allocated.

Trying to access it might cause a Segmentation fault.


Common way to end up with a dangling pointer:
char* func()
{
char str[10];
strcpy(str,"Hello!");
return(str);
}
//returned pointer points to str which has gone out of scope.

You are returning an address which was a local variable, which would have gone out of scope by the
time control was returned to the calling function. (Undefined behaviour)
Another common dangling pointer example is an access of a memory location via pointer, after free
has been explicitly called on that memory.
int *c = malloc(sizeof(int));
free(c);
*c = 3; //writing to freed location!

A memory leak is memory which hasn't been freed, there is no way to access (or free it) now, as
there are no ways to get to it anymore. (E.g. a pointer which was the only reference to a memory
location dynamically allocated (and not freed) which points somewhere else now.)
void func(){
char *ch;
ch = (char*) malloc(10);
}
//ch not valid outside, no way to access malloc-ed memory

Char-ptr ch is a local variable that goes out of scope at the end of the function, leaking the
dynamically allocated 10 bytes.
shareimprove this answer

edited Jul 19 '13 at 7:53

answered Oct 30 '12 at 4:49

glglgl

Anirudh Ramanathan

43.9k44893

28.1k961109

This article might also be helpful stackoverflow.com/questions/127386/


add a comment

up
vote16do
wn vote

You can think of these as the opposites of one another.


When you free an area of memory, but still keep a pointer to it, that pointer is dangling:

char *c = malloc(16);
free(c);
c[1] = 'a'; //invalid access through dangling pointer!

When you lose the pointer, but keep the memory allocated, you have a memory leak:
void myfunc()
{
char *c = malloc(16);
} //after myfunc returns, the the memory pointed to by c is not freed: leak!
shareimprove this answer

answered Oct 30 '12 at 4:54

Greg Inozemtsev
2,959915

add a comment
up
vote10do
wn vote

A dangling pointer is one that has a value (not NULL) which refers to some memory
which is not valid for the type of object you expect. For example if you set a pointer to
an object then overwrote that memory with something else unrelated or freed the
memory if it was dynamically allocated.
A memory leak is when you dynamically allocate memory from the heap but never free
it, possibly because you lost all references to it.
They are related in that they are both situations relating to mismanaged pointers,
especially regarding dynamically allocated memory. In one situation (dangling pointer)
you have likely freed the memory but tried to reference it afterwards; in the other
(memory leak), you have forgotten to free the memory entirely!
shareimprove this answer

answered Oct 30 '12 at 4:50

maerics
61.2k11112167

add a comment
up
vote5do
wn vote

*dangling pointer *
If any pointer is pointing the memory address of any variable but after some variable has
deleted from that memory location while pointer is still pointing such memory location.
Such pointer is known as dangling pointer and this problem is known as dangling pointer
problem.
#include
int *call();
void main(){
int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);
}
int * call(){
int x=25;
++x;
return &x;
}

Output: Garbage value Note: In some compiler you may get warning message returning
address of local variable or temporary
Explanation: variable x is local variable. Its scope and lifetime is within the function call
hence after returning address of x variable x became dead and pointer is still pointing ptr
is still pointing to that location.
Solution of this problem: Make the variable x is as static variable. In other word we can
say a pointer whose pointing object has been deleted is called dangling pointer.
Memory Leak
In computer science, a memory leak occurs when a computer program incorrectly
manages memory allocations. As per simple we have allocated the memory and not Free
other language term say not release it call memory leak it is fatal to application and
unexpected crash.
shareimprove this answer

answered Jul 19 '13 at 6:32

PeterParker
28348

add a comment
up
vote5do
wn vote

Memory leak: When there is a memory area in a heap but no variable in the stack
pointing to that memory.
char *myarea=(char *)malloc(10);
char *newarea=(char *)malloc(10);
myarea=newarea;

Dangling pointer: When a pointer variable in a stack but no memory in heap.


char *p =NULL;

A dangling pointer trying to dereference without allocating space will result in a


segmentation fault.
shareimprove this answer

edited Jul 28 '14 at 19:12

answered Jul 28 '14 at 19:06

drs

user2264571

1,40611427

5111

your example for dangling pointer is not really a dangling pointer but a NULL pointer. The correct example would be
to dynamically assign memory to pointer, say using malloc(), and then free() that memory, this makes it a dangling
pointer. NOTE: after freeing we haven't assigned it to NULL thus pointer still points to same memory address which
makes it a dangling pointer. Now, if you try to access that memory using the same pointer (i.e. dereference the pointer)
you may end of getting segmentation fault. sactiw Sep 16 '14 at 5:54
add a comment
up
vote2do
wn vote

Pointer helps to create user defined scope to a variable, which is called Dynamic variable.
Dynamic Variable can be single variable or group of variable of same type ( array) or
group of variable of different types ( struct). Default local variable scope starts when
control enters into a function and ends when control comes out of that function. Default
global vairable scope starts at program execution and ends once program finishes.
But scope of a dynamic variable which holds by a pointer can start and end at any point in
a program execution, which has to be decided by a programmer. Dangling and memory
leak comes into picture only if a programmer doesnt handle the end of scope.
Memory leak will occur if a programmer, doesnt write the code ( free of pointer) for end
of scope for dynamic variables. Any way once program exits complete process memory
will be freed, at that time this leaked memory also will get freed. But it will cause a very
serious problem for a process which is running long time.
Once scope of dynamic variable comes to end(freed), NULL should be assigned to pointer
variable. Otherwise if the code wrongly accesses it undefined behaviour will happen. So
dangling pointer is nothing but a pointer which is pointing a dynamic variable whose
scope is already finished.
shareimprove this answe

You might also like