How many memory allocations do you have?
memory allocation for static storage stack heap
1. Allocate memory from a static storage area. When the program compiles, the memory is already allocated and exists during the entire run of the program, such as global variables.
2, created on the stack. When executing a function, the storage unit of the local variable within the function can be created on the stack, which is automatically freed when the function ends.
The processor's specified set has an allocation operation on the stack memory, so it is more efficient, but the allocated memory capacity is limited.
3, allocating memory on the heap, also known as dynamic memory allocation, when the program is running with the malloc function or the new operator to request any size of memory, the programmer
Use the free function or the delete operator to release memory. Dynamic memory usage is very flexible, but there are many problems.
1 //main.c2 intA =0;//Global Initialization Zone3 Char*P1;//Global Uninitialized Zone4 5 Main ()6 { 7 Static intc =0;//Global (Static) initialization zone8 intb//Stack9 CharS[] ="ABC";//StackTen Char*P2;//Stack One Char*P3 ="123456";//"123456\0" in the constant area, p3 on the stack. AP1 = (Char*)malloc(Ten); -P2 = (Char*)malloc( -);//areas that are allocated 10 and 20 bytes are in the heap area. -}
C Language Memory allocation problem simple understanding