I am hereThis articleIn this article, four dynamic memory management functions in C language are explained in detail. In this article, I will explain the 6 most likely mistakes that beginners make when using these 4 functions, and how to avoid them. These 6 easy mistakes are:
- Dereference operation to NULL pointer.
- Out-of-bounds access to dynamic memory.
- Forgot free.
- Free twice for the same space.
- free part of dynamic memory.
- free non-dynamic open memory.
I hope that after you finish reading this article, don’t make similar mistakes.
1. Dereference operation of NULL pointer
When you malloc a piece of space, it is possible to fail to open it. Once it fails, malloc returns a NULL pointer. If the return value of malloc is not judged, the pointer returned by malloc is directly used, which may lead to a dereference operation to the NULL pointer. For example:
int* p = (int*)malloc(10 * sizeof(int)); for (int i = 0; i < 10; i++) { p[i] = i + 1; }
Since malloc may return a NULL pointer, once p is NULL, dereferences to it are very dangerous!
Solution: Every time you use malloc, calloc, realloc and other functions, be sure to determine whether the return value is NULL.
int* p = (int*)malloc(10 * sizeof(int)); if (p == NULL) { // Error handling perror("malloc"); exit(-1); } // At this time, p must not be NULLfor (int i = 0; i < 10; i++) { p[i] = i + 1; }
2. Out-of-bounds access to dynamic memory
Suppose you use malloc to apply for 40 bytes of space, you must remember the size of this space and cannot be accessed beyond the boundary. for example:
int* p = (int*)malloc(10 * sizeof(int)); if (p == NULL) { // Error handling perror("malloc"); exit(-1); } for (int i = 0; i <= 10; i++) { p[i] = i + 1; }
In the above code, when i==10, p[i] is equivalent to *(p+10), which has exceeded the 40 bytes of space applied, which is very dangerous!
Solution: In any case, when accessing memory with pointers, be sure to observe whether it is out of bounds! For example, the above code can only access the space of p[0]~p[9].
3. Forgot free
If you use functions such as malloc to open up space, but there is no free, it will cause memory leaks! This situation is very uncomfortable for programmers and must be avoided.
Solution: For each piece of dynamic memory, you must free it after use. for example:
int* p = (int*)malloc(10 * sizeof(int)); if (p == NULL) { perror("malloc"); exit(-1); } // use// ... free(p); p = NULL;
4. Free twice for the same space
If you have freed a piece of space, if you free it again, then won’t you do this in one go! Not only that, if the pointer is not set to NULL after the first free, the pointer is a wild pointer for the second free, which is very dangerous!
int* p = (int*)malloc(10 * sizeof(int)); if (p == NULL) { perror("malloc"); exit(-1); } // use// ... free(p); // ... free(p);
Solution: Just free it once, what do you do to free it twice!
It is worth noting that since the free(NULL); when free(NULL);, the free function does nothing, so the following code will not have any problems, but it is not recommended to write this way (it feels like you are full and full).
// ... free(p); p = NULL; free(p);
Part of dynamic memory
Note that when free a pointer, this pointer must point to the starting position of dynamic memory! For example, in the following code, if this pointer no longer points to the starting position, it is also very dangerous to free it.
int* p = (int*)malloc(10 * sizeof(int)); if (p == NULL) { perror("malloc"); exit(-1); } // ... p += 5; // ... free(p); p = NULL;
Since after malloc and before free, the pointer p no longer points to the starting position of dynamic memory, there will be problems if free drops p.
Solution: Since you need to know the starting address of dynamic memory when free, it is not recommended to change the pointing of p when using this memory. In addition, if you use realloc to expand capacity, you must update p and always let p point to the starting address of dynamic memory to facilitate free release.
int* p = (int*)malloc(10 * sizeof(int)); if (p == NULL) { perror("malloc"); exit(-1); } // ... // Use it at all times to let p point to the starting position of dynamic memory!int* tmp = (int*)realloc(p, 20 * sizeof(int)); if (tmp == NULL) { free(p); p = NULL; perror("realloc"); exit(-1); } else { p = tmp; } // ... free(p); p = NULL;
Non-dynamic memory
Free is used to free up dynamically opened memory. So, if this memory space is not dynamically opened with malloc, calloc, and realloc, don't free it.
int arr[10] = {0}; // ... int a = 0; int* pa = &a; // ... free(arr); arr = NULL; free(pa); pa = NULL;
Solution: Don’t write such code. When free, you have to see clearly whether this space is a dynamically opened up space and always keep your mind clear.
Summarize
6 easy mistakes and solutions to C language dynamic memory management:
1. Dereference operation of NULL pointer. This is because functions such as malloc may return NULL, so the return value needs to be checked.
2. Out-of-bounds access to dynamic memory. Always remember how big this space is, don’t cross the line.
3. Forgot free. Just don't forget.
4. Free twice for the same space. Just free once, and free again is an unnecessary move.
Part of dynamic memory. Always let a pointer remember the start address of this dynamic memory.
Non-dynamic memory. Keep your mind clear, only the spaces that come out of malloc, calloc, and realloc need free.
This is the article about 6 mistakes that beginners are likely to make in C language dynamic memory management. For more relevant content on C language dynamic memory management, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!