The difference between a dangling pointer and a memory leak

I do not understand the difference between a dangling pointer and a memory leak. How are these two terms related?

+70
c
Oct 30
source share
6 answers

A dangling pointer indicates an already freed memory. Storage is no longer allocated. Attempting to access it may result in a segmentation error.

The usual way to get into the dangling pointer:

char* func() { char str[10]; strcpy(str,"Hello!"); return(str); } //returned pointer points to str which has gone out of scope. 

You return an address that was a local variable that would go out of scope, and the time control was returned to the calling function. (Undefined behavior)

Another common example of a pointer to a dangling pointer is accessing a memory location using a pointer after it has been explicitly free in it.

 int *c = malloc(sizeof(int)); free(c); *c = 3; //writing to freed location! 



A memory leak is a memory that has not been freed, there is no way to access (or free of charge) now, since there are no ways to get to it anymore. (For example, a pointer that was the only reference to a dynamically allocated memory cell (and not freed), which now points somewhere else.)

 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 beyond the scope at the end of the function and flows dynamically allocated 10 bytes .

+124
Oct 30
source share

You can consider them as opposites of each other.

When you free a memory area, but still keep a pointer to it, this pointer hangs:

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

When you lose the pointer but keep the allocated memory, 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! 
+19
Oct 30
source share

A sagging pointer is a value that has a value (not NULL) that refers to some memory that is not valid for the type of object you expect. For example, if you pointed a pointer to an object, then overwritten this 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 you never free it, possibly because you have lost all references to it.

They are due to the fact that both of them are situations associated with an incorrect pointer, especially in relation to dynamically allocated memory. In one situation (dangling pointer), you most likely freed up memory, but tried to reference it later; in another (memory leak), you forgot to completely free the memory!

+11
Oct 30 '12 at 4:50
source share

Hanging pointer

If any pointer indicates the memory address of any variable, but after any variable has been deleted from this memory location, the pointer still points to that memory location. Such a pointer is known as a dangling pointer, and this problem is known as a dangling pointer problem.

 #include<stdio.h> 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. On some compilers, you may receive a warning message that returns the address of a local variable or temporary

Explanation: The variable x is a local variable. Its scope and lifetime are inside the function call, so after returning the address of the variable xx became dead, and the pointer still indicates that ptr still points to this location.

The solution to this problem is to make the variable x the same static. In other words, we can say that the pointer whose object was deleted is called the dangling pointer.

Memory leak

In computer science, a memory leak occurs when a computer program incorrectly manages memory allocations. In accordance with a simple one, we allocated memory, and not the Free language of another language, say, do not release its memory leak, it is harmful for the application and an unexpected crash.

+5
Jul 19 '13 at 6:32
source share

A pointer helps you create a custom scope for a variable called a dynamic variable. A dynamic variable can be a single variable or a group of variables of the same type ( array ) or a group of variables of different types ( struct ). The local variables area starts by default when the control enters the function and ends when the control exits the function. The global virtual area starts by default when the program runs and ends when the program ends.

But the scope of the dynamic variable that holds the pointer can begin and end at any time during program execution, which must be decided by the programmer. Damage and memory leak come into picture only if the programmer does not process the end of the area.

A memory leak will occur if the programmer does not write a code ( free pointer) to complete the area for dynamic variables. Any way that a program exits the full memory of a process will be freed, at that time this leaked memory will also be freed. But this will cause a very serious problem for a process that works for a long time.

As soon as the scope of a dynamic variable ends (is freed), NULL should be assigned to a pointer variable. Otherwise, if the code mistakenly accesses it, undefined behavior will be executed. So a dangling pointer is nothing more than a pointer pointing to a dynamic variable whose scope has already been completed.

+3
Jul 19 '13 at 6:56
source share

Memory leak . If the heap has a region of memory, but there is no variable on the stack pointing to that memory.

 char *myarea=(char *)malloc(10); char *newarea=(char *)malloc(10); myarea=newarea; 

Dangling pointer : when a pointer variable is on the stack, but not memory on the heap.

 char *p =NULL; 

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

+3
Jul 28 '14 at 19:06
source share



All Articles