Program memory space
Code area ............ (Code Area)
All-bureau data ................ (Data Area)
Pile area ................ (Heap Area)
Stack area ................ (Stack Area)
A program assigns the operating system to its running memory block to 4 areas:
1. Code area: The code of the program is stored, that is, the various function code blocks in the program.
2. Global Data Area: The global data and static data of the program.
3. Pile area: The dynamic data of the storage program.
4. Stack area: The local data of the program, that is, the data in each function.
◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
The stack is a data structure, and its working principle is like a bullet in the bullet, the first bullet is going to wait until it is only flying out, and the last pressing bullet first flying out
The C function calling process requires calling initialization and afterwards. The entire process of function call is the process of stack spatial operation. When the function is called, first: 1. Build a stack room for the modulated function. 2. Protect the operating status of the call function and return the address. 3. Transfer parameters. 4. The control is transferred to the modulated function.
The stack operation principle of the function is consistent, but the specific implementation is different from the compiler.
Note: The stack is a limited resource. Each time you call a function, the established stack space is smaller than the stack space of the previous function. It can be seen that if a layer is called, or too many definitions of local variables, especially arrays, it may eventually cause the stack space to deplete the program to run an error. If the program does to occupy a considerable stack space, you can improve the size of the stack space before the connection.
◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
1. Pile of memory: Heap is a memory space. Heap, allowing the program to apply for a certain size memory space when running (not when compiling).
The program does not determine the memory space acquired in the run when compiling and connects, this memory environment is small as the program runs, and this memory is a heap memory, so the stack is dynamic. Heap memory is also called dynamic memory.
2. Get a stack memory: Function Malloc () is a method of obtaining a heap memory in a C program, which is declared in alloc.h header files. The prototype of the malloc () function is: void * malloc (size_t size); size_t Unsigned Long
This function returns the address of the memory from the "cut" Size size from the heap memory. The content in the memory is unknown.
note! The program cannot be guaranteed to obtain the required memory from the heap. Sometimes, the system can provide insufficient assignment, and the system will return an empty pointer value NULL. At this time, all access to this pointer is destructive, so call the malloc () function should be as follows: