In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the explanation of knowledge points about dynamic memory allocation in C language". In daily operation, I believe that many people have doubts about the explanation of knowledge points about dynamic memory allocation in C language. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "knowledge points about dynamic memory allocation in C language". Next, please follow the editor to study!
Catalogue
1. Malloc and free functions
II. Calloc
III. Realloc
Fourth, common errors in dynamic memory
In this installment, we will cover the malloc, calloc, realloc, and free functions.
This is the header file of a dynamic memory allocation function.
C language dynamic memory allocation function, some beginners of the c language can not help but ask: why do we use functions to achieve dynamic memory allocation?
First of all, let's familiarize ourselves with the memory of the computer. There are roughly these four memory areas in the computer system:
1) Stack: store some local variables and formal parameters (formal parameters) that we define in the stack
2) character constant area: it mainly stores some character constants, such as char * p = "hello world", in which "hello world" is stored in the character constant area.
3) Global area: store some global and static variables in the global area
Heap: heap is mainly through dynamically allocated storage space, that is, the dynamic allocation of memory space that we need to talk about next.
Comparison of static memory and dynamic memory:
Static memory is automatically allocated and released by the system. Static memory is allocated on the stack. (for example, local variables in a function)
Dynamic memory is manually allocated and freed by programmers. Dynamic memory is allocated on the heap. (for example, when you write a linked list in C language, you need to allocate memory space for Node nodes.)
1. Malloc and free functions
Void* malloc (size_t * * size)
Return type: void*, that is, the function can return all types of pointer form. All you need to do is cast when you open up space.
Function argument: size_t size, which tells the function how many bytes of memory you need to open up.
Void free (void* memblock)
No parameters are returned.
Function argument: void* memblock, the free function can receive dynamically allocated memory space from all types of pointers.
Describe it all in terms of chestnuts:
# include # include int main () {/ / Open up 10 spaces of int type int* arr = (int*) malloc (10 * sizeof (int)); / / remember that the size given here is 10 * int (4 bytes) int I = 0; if (arr = = NULL) {perror ("malloc"); / / if malloc fails to open up space, malloc will return NULL return 1 } for (I = 0; I
< 10; i++) *(arr + i) = i; //放入数据 0 …… 9 for (i = 0; i < 10; i++) printf("%d ",*(arr + i)); //记得释放所开辟的空间 free(arr); return 0;}二、calloc void* calloc (size_t num, size_t** size ); 返回类型:与malloc函数是一样的,就不在多说了。 函数参数:size_t num, 需要开辟多少个元素的空间。 size_ size, 每一个元素,所占用的内存空间是多少个字节。 注:与函数 malloc 的区别只在于 calloc 会在返回地址之前把申请的空间的每个字节初始化为全0。 栗子: #include #include int main(){ //还是申请10个int类型的内存空间 int* arr = (int*)calloc(10, sizeof(int)); if (arr == NULL) { perror("calloc"); //calloc开辟空间的话,会返回NULL return 1; } //不做赋值运算,直接输出刚开辟的空间,看是否是已经初始化为0了 int i = 0; for (i = 0; i < 10; i++) printf("%d ",*(arr + i)); //记得释放空间 free(arr); return 0;}三、realloc void* **realloc(*void memblock, size_t size); 作用: Reallocate memory blocks.(重新分配内存块) memblock是需要调整的内存地址 size调整之后新大小 返回值为调整之后的内存起始位置。 这个函数调整原内存空间大小的基础_上,还会将原来内存中的数据移动到新的空间。 realloc在调整内存空间的是存在两种情况: 情况1 :原有空间之后有足够大的空间Suppose I also want to double the memory space in the red box, and there is enough space at the back of this space. All realloc functions will open up space immediately after the red box. And the address of the first element of the red box is returned.
Case 2: there is not enough space behind the original space.
At this time, if I still want to expand the memory space of the red box, and the space immediately behind the red box has been occupied by other programs, if I want to open up the space, I can only release this space first (realloc will be automatically released), and then go to other larger places to open up space.
As shown in the figure:
Note: realloc function, there is a very noteworthy place, see the following code:
Int main () {int* arr = (int*) malloc (5 * sizeof (int)); / / first open up five spaces of int type if (arr = = NULL) return 1; / / at this time, I think malloc has opened up less space, I want to add arr = (int*) realloc (arr, 10); free (arr); return 0;}
What do you think is the drawback of this code?
Analysis:
In line 8, the realloc function adjusts the space of the arr. He first checks to see if the memory space behind the arr is enough, and if not, he will look for other larger places to open up space. Suppose my memory is full at this time, and realloc returns NULL.
In other words, I originally wanted to increase capacity, but did not succeed, and lost the data in the previous space.
So when using the realloc function, first use a temporary variable to save, if the return is not NULL, we can assign the returned memory address to arr.
As follows:
Int main () {int* arr = (int*) malloc (5 * sizeof (int)); / / first open up 5 spaces of int type if (arr = = NULL) return 1; / / at this time, I think malloc has opened up less space, I want to add int* tmp = NULL; tmp = (int*) realloc (arr, 10); if (tmp! = NULL) arr = tmp; free (arr) Return 0;} IV. Common dynamic memory errors
Dereferencing NULL
Int main () {int* arr = (int*) malloc (10 * sizeof (int)); * arr = 10; / / No NULL judgment for arr free (arr); return 0;}
Release free for space that is not dynamically allocated
Int main () {int a = 10; int* pa = & a; free (pa); / / pa pointer is not the space opened up by functions such as malloc, and cannot be released using free. The system will automatically reclaim return 0;}
Use the free function to release a portion of a dynamically allocated space
Int main () {int* arr = (int*) malloc (10 * sizeof (int)); if (arr = = NULL) return 1; arr++; / / at this point, arr jumped back 4 bytes free (arr); / / now to free space, the first 4 bytes of space will not be released, return 0 will be reported;}
Release the same memory space multiple times
Int main () {int* arr = (int*) malloc (10 * sizeof (int)); if (arr = = NULL) return 1; free (arr); free (arr); / / repeated release of return 0;}
Dynamically opened space forgot to release (memory leak)
Int main () {int* arr = (int*) malloc (10 * sizeof (int)); if (arr = = NULL) return 1; / there is no free space, which will cause memory leak / / cause memory leak. There are many reasons, for example, when calling other functions, if you want to return to this function, the pointer is not correct, resulting in the open space not coming back, etc. Return 0;}
Note: dynamically open up memory space, be sure to release. Otherwise the consequences will be very serious!
The update of this issue is over! I'll see you next time.
Main ()
{
Int* arr = (int*) malloc (10 * sizeof (int))
If (arr = = NULL)
Return 1
}
/ / No free space will cause memory leak / / cause memory leak for many reasons, for example, when calling other functions, if you want to return to this function, the pointer is not used correctly, resulting in return 0 that the opened space is not returned, etc.
Note: dynamically open up memory space, be sure to release. Otherwise the consequences will be very serious!
At this point, the study of "explaining the knowledge points about dynamic memory allocation in C language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.