Abstract: The C language is the pointer, which can be used for linked list operations. When it comes to linked lists, most of the time, dynamic allocation rather than static allocation is used to allocate memory for this purpose.
This article is shared from Huawei Cloud Community " creation] The Essence of Dynamic Memory Allocation in C Language ", author: G-washington.
C language is a process-oriented, abstract general-purpose programming language, which is widely used in low-level development. Although the C language provides many low-level processing functions, it still maintains cross-platform features. Because of its portability, extensibility, and reusability, the C language still occupies a certain position on the programming language rankings. Advantageous position. The more important thing in the C language is the pointer, which can be used for linked list operations. When it comes to linked lists, most of the time, dynamic allocation rather than static allocation is used to allocate memory for this purpose.
The concept of memory allocation
Variables (or objects) are usually defined, and the compiler can know the size of the required memory space according to the type of the variable (or object) at compile time, so that the system allocates a certain storage space for them at an appropriate time. This kind of memory allocation is called static storage allocation; some operation objects can only be determined when the program is running, so that storage space cannot be reserved for them at compile time. Only when the program is running, the system allocates memory according to runtime requirements. This method is called dynamic storage allocation. All dynamic storage allocation is performed in the heap area.
Memory is not inexhaustible. 4g, 8g, and 16g are common computer memory sizes. Open the task manager and you can see the memory occupied by different applications. If an application occupies most of the memory, it is estimated that the resources of other applications are tight, then this application may be uninstalled, find a memory-saving one.
Memory management is an operation close to the physical nature of the computer, and the actions under the programming language must eventually be realized by mobilizing the memory. System resources are not unlimited, and the programs running on the system are not the only one. If memory is ignored, dangerous and redundant code products will be designed, or there will be no better interaction.
The characteristics of dynamic memory allocation
Dynamic memory is relative to static memory. The so-called dynamic and static refers to the way the memory is allocated. Dynamic memory refers to memory allocated on the heap, while static memory refers to memory allocated on the stack. The essence of dynamic memory allocation is that when a piece of memory is needed, this piece of memory is allocated; when a piece of memory is no longer needed, this piece of memory can be released. This flexible memory allocation method is just suitable for the data structure of the linked list.
Disadvantages of traditional arrays
Compared with dynamic memory allocation, arrays have the following disadvantages:
- The length of the array must be specified in advance, and it can only be a constant, not a variable.
- Because the length of the array can only be constant, its length cannot be dynamically expanded or reduced during the running of the function.
- The memory space occupied by the array cannot be manually released by programmers, but can only be automatically released by the system after the function runs. Therefore, the array defined in a function can only be used by other functions during the running of the function.
The problem of "traditional arrays" is actually a problem of static memory. But dynamic memory does not have this problem, because dynamic memory is manually programmed and released by programmers, so you can release it whenever you want. As long as the programmer does not manually program and release it, even if the function ends, the dynamically allocated memory space will not be released, and other functions can continue to use it. Unless it is the end of the entire program, all the memory space allocated by the system for the program will be released at this time.
Application and release of dynamic memory
The application and release of dynamic memory mainly rely on two functions malloc and free. malloc is a system function, it is the abbreviation of memory allocate. Among them, memory means "memory", and allocate means "allocation". As the name suggests, the function of the malloc function is to "allocate memory". To call it, the header file <stdlib.h> must be included.
The malloc() function will apply for a continuous free memory space in the heap; if the application is successful, it will return a pointer to this memory space, if it fails, it will return NULL, so we use the malloc() function to open up the dynamic memory. , It is necessary to judge whether the return value of the function is NULL; the return value type is void , the malloc() function does not know what type of data is stored in the size bytes continuously opened, so we need to decide by ourselves, the method is Before malloc(), strengthen the system and transform it into the type we need, such as: (int )malloc(sizeof(int)*n).
Next, use the malloc function to write a program. The function of the program is to call the called function and magnify the data in the memory dynamically allocated in the calling function by 10 times.
The output result is: *p = 100
Free is a release function. The memory space applied in the heap will not be the same as the local variables stored in the stack. The memory will be automatically released after the function is called. If we do not manually release it, it will not be released until the end of the program, which may be Causes a memory leak, that is, the data in this piece of memory in the heap is no longer used, but it always occupies this space, so when the dynamic memory we apply for is no longer used, it must be released in time. However, it should be noted that, Freeing does not mean emptying the memory space, but marking the memory space as "available" so that the operating system can reallocate it to other variables when it allocates memory.
So, when the pointer variable is released, what will happen to the data in the memory space it points to? The standard behavior of free just means that this memory can be redistributed, and there is no mandatory requirement as to whether the data in it is emptied. Different compilers may handle it differently. Let's take a look at how the VC++6.0 compiler handles:
It can be seen that in VC++ 6.0, when the pointer variable is released, although it still points to that memory space, the value in that memory space will be reset to a very small negative number. The dynamically created memory must be released if it is not used. Note that a dynamic memory can only be released once. The program will crash if it is released multiple times, because it has already been released and cannot be released a second time.
In summary, malloc and free must exist in pairs and correspond to each other. There must be free if there is malloc, and there must be several free if there are several mallocs. At the same time, every time a pointer variable that points to dynamic memory is released, it must be pointed to NULL immediately.
Precautions
1) Freeing a part of a memory is not allowed. The dynamically allocated memory must be released in its entirety. However, the realloc function can shrink a dynamically allocated memory and effectively release part of the memory at its tail.
2) Do not access the memory that has been released by the free function. Suppose that a pointer to dynamically allocated memory is copied, and several copies of this pointer are scattered throughout the program. You cannot guarantee that when you use one of the pointers, the memory pointed to by it has been released by the other pointer. Also make sure that all places in the program that use this memory stop using it before the memory is released.
3) When the dynamically allocated memory is no longer needed, it should be released so that it can be re-allocated for use. Allocating memory but not releasing it after use will cause a memory leak.
Click to follow and learn about Huawei Cloud's fresh technology for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。