Problems with memory management in C Language heap, memory leakage, usage of wild pointers, illegal release of pointers, and memory management pointers
Problems with memory management in C Language heap, memory leakage, usage of wild pointers, illegal release of pointers
(1) The opened memory is not released, causing memory leakage.
(2) The Wild pointer is used or released.
(3) Invalid Pointer release
(1) The opened memory is not released, causing memory leakage. The following example may cause 20 bytes leakage. Memory leakage is not an error that will immediately cause a fault,
It will consume system memory.
void function1(){char *pa;pa = (char*)malloc(sizeof(char)*20);if(NULL !=pa){strcpy(pa,”hello”);printf(“pa = %x\n”,(unsigned int)pa);printf(“pa = %s\s”,pa);}return;}
(2) The Wild pointer is used or released.
A wild pointer is a released memory pointer, which is directed to a free or realloc function, but the pointer is still in use.
void function2(){char *pa;pa = (char*)malloc(sizeof(char)*20);if(NULL !=pa){strcpy(pa,”hello”);printf(“pa = %x\n”,(unsigned int)pa);printf(“pa = %s\s”,pa);}free(pa);printf(“ pa = %s”,pa);return;}
The correct memory release should be the following
void function2(){char *pa;pa = (char*)malloc(sizeof(char)*20);if(NULL !=pa){strcpy(pa,”hello”);printf(“pa = %x\n”,(unsigned int)pa);printf(“pa = %s\s”,pa);}free(pa);pa = NULL;if(NULL != pa){printf(“pa = %s\n”,(unsigned int)pa);}return;}
(3) Invalid Pointer release
void function3(){char a[20];int b;free(a);free(&b);return;}
In the above program, a [20] is an array on the stack, a is the address of the memory, B is a variable on the stack, and B is its address. Memory on these stacks, Compiler
Resources will be automatically managed and recycled. Releasing them using free in a program is a wrong way.
char *pa;pa = (char*)malloc(sizeof(char)*20);free(pa);free(pa);
In the previous program, releasing the memory twice is incorrect because after the first release, the address has changed to unallocated heap memory, the free function cannot release unallocated heap memory.
char *pa;char *pa;pa = (char*)malloc(sizeof(char)*20);pb = pa++;free(pb);
In the above program, although pa is a heap memory pointer allocated, pb is used as the address of pa plus 1, it is also a heap memory pointer, the pointer also points to allocated memory. However, the memory pb is still invalid. This is because this pointer is not allocated from malloc, but a pointer value in the middle.
C language pointer question-blank question: Why is memory leakage?
Because the str variable defined in the test function has been applied for space in it, but when the function ends, str ends, so the requested space cannot be released, which naturally leaks !~
C ++ Memory leakage, meaning of the wild pointer
Let's give you an example.
Char * p1 = new char [10];
Char * p2 = p1;
At this time, you released p1.
Delete [] p1;
P1 = NULL
But p2 is still not NULL at this time, and it points to a memory that has been released by you, which becomes a wild pointer.