catalogue
1. Why does dynamic memory management exist
2. Introduction to dynamic memory function
3 common errors in dynamic memory allocation i
3.1 dereference of NULL pointer
3.2 cross border access to dynamic development space
3.3 free release for non dynamic development memory
3.4} use free to release a part of dynamic memory
3.5 release the same dynamic memory multiple times
3.6 dynamic memory forgetting release (memory leakage)
1. Why does dynamic memory management exist
The memory development method we already know is
int arr[100]..int a.
However, in the actual project, this memory management method is extremely inconvenient. For example, the address book made before has opened up a lot of space at once, but the elements inside are not actually used so much, and the memory will be wasted. If the memory is too small, it is certainly not enough. Here, c language provides dynamic memory.
2. Introduction to dynamic memory function
2.1 malloc
int main() { //Code 1 int num = 0; scanf("%d", &num); int arr[num] = {0}; //Code 2 int* ptr = NULL; ptr = (int*)malloc(num*sizeof(int)); if(NULL != ptr)//Determine whether the ptr pointer is null { int i = 0; for(i=0; i<num; i++) { *(ptr+i) = 0; } } free(ptr);//Freeing the dynamic memory pointed to by ptr ptr = NULL;//Is it necessary? return 0; }
In the last step above, ptr=NULL is actually necessary, because after the ptr space is released, the ptr still points to that address, but the elements inside are not defined. That is, the hotel check-out problem.
2.2 calloc
Another memory development function is calloc
2.3 realloc
int main() { int *p=(int *)calloc(10,sizeof(int)); if (p != NULL) { printf("Memory development succeeded\n"); } int i = 0; for (i = 0; i < 10; i++) { printf("%d ", p[i]); } //There is not enough memory and capacity needs to be increased. The capacity increase here is developed in the original calloc, and it is increasing to 20 shaping int* ptr = (int*)realloc(p, 80); if (ptr != NULL) { p = ptr; } for (i = 10; i < 20; i++) { p[i] = i; } for (i = 0; i < 20; i++) { printf("%d ", p[i]); } free(p); return 0; }
3 common errors in dynamic memory allocation i
3.1 dereference of NULL pointer
void test() { int *p = (int *)malloc(INT_MAX/4); *p = 20;//If the value of p is NULL, there is a problem free(p); }
3.2 cross border access to dynamic development space
void test() { int i = 0; int *p = (int *)malloc(10*sizeof(int)); if(NULL == p) { exit(EXIT_FAILURE); } for(i=0; i<=10; i++) { *(p+i) = i;//Cross border access when i is 10 } free(p); }
3.3 free release for non dynamic development memory
void test() { int a = 10; int *p = &a; free(p);//ok? }
3.4} use free to release a part of dynamic memory
void test() { int *p = (int *)malloc(100); p++; free(p);//p no longer points to the beginning of dynamic memory }
3.5 release the same dynamic memory multiple times
void test() { int *p = (int *)malloc(100); free(p); free(p);//Repeated release }
3.6 dynamic memory forgetting release (memory leakage)
void test() { int *p = (int *)malloc(100); if(NULL != p) { *p = 20; } } int main() { test(); while(1); }
Forgetting to release dynamically opened space will cause memory leakage,
It should be noted that dynamic memory must be released correctly;