Semi-dynamic linux C memory management and linux memory management
What is the first reaction when I see a semi-Dynamic Request for memory?
In fact, the semi-dynamic memory application is easy to understand. It is implemented using the alloca function in gnu c.
# Include <stdlib. h> void * alloca (size_t size)
The alloca function can dynamically apply for memory like malloc (note that the applied memory is located in the stack of the called function, and the page is automatically released after the function is called)
The advantage is obvious: You can freely apply for space without manually releasing the memory.
The downside is that when an error occurs, you cannot get a clear error message. In addition, non-GNU systems do not support alloca, which is a BSD extension with poor compatibility. (Other platforms may use C to write a simulated alloca, but the efficiency is not good)
In gnu c, you can replace alloca with an array of variable sizes.
Difference: an array of variable size is only valid in the region where the array name is located. If it exceeds the value, it is released. The reserved function ends when alloca applies.
Alloca can be used in the loop body. memory space is gradually increased in each loop. The variable-size array does not work.