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.
PeterParker Jul 19 '13 at 6:32 2013-07-19 06:32
source share