A static memory allocation
1. Global variables for C and static local variables are determined at compile time, and the actual allocation of the stored space is completed before the program starts executing.
2. Created on the stack, such as local automatic variables, assign a storage unit to the compound statement where the variable definition is executed. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.
The advantage of statically arranging storage is that it is more convenient and efficient to achieve
Two dynamic memory allocations
1. Allocation from the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by the programmer and is very flexible to use, but if space is allocated on the heap, it is the responsibility to reclaim it, otherwise the running program will have a memory leak, and frequently allocating and releasing different sizes of heap space will result in heap fragments.
Memory storage area in three C
1. Stack (stack) is automatically allocated by the compiler to release the local variables, function parameters, return data, return addresses, etc., which are allocated for running functions. It operates in a manner similar to a stack in a data structure.
2, heap area (heap) is usually allocated by the programmer release, if the programmer does not release, the program ends may be recycled by the OS. The allocation method is similar to a linked list.
3. Global zone (static zone), which holds global variables, static data, constants. System released after the program is finished
4, the literal constant area constant string is put here. Released by the system after the program is finished.
5. The program code area holds binary code for the function body (class member functions and global functions).
four heap vs. stack comparison1.How to apply
Stack: Automatically assigned by the system. For example, declare a local variable int b in the function; The system automatically opens up space for B in the stack.
Heap: Requires the programmer to apply himself and indicate the size of the malloc function in C, which is the new operator in C + +.
such as P1 = (char *) malloc (10); P1 = new CHAR[10];
such as P2 = (char *) malloc (10); P2 = new char[20];//here to note that P1 and P2 themselves are in the stack.
2. Response of the system after application
Stack: As long as the remaining space of the stack is larger than the requested space, the system will provide memory for the program, otherwise it will report the exception prompt stack overflow.
Heap: First of all should know that the operating system has a record of the free memory address of the list, when the system receives the application of the program, it will traverse the list, look for the first space is larger than the requested space of the heap node, and then delete the node from the list of idle nodes, and the node's space allocated to the program.
for most systems, the size of this allocation is recorded at the first address in the memory space, so that the DELETE statement in the code can properly free up the memory space. because the size of the found heap node does not necessarily equal the size of the request, the system automatically re-places the extra portion into the idle list.
3. restrictions on size of applications
Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in WINDOWS, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.
Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.
2016-2-16 dynamic memory allocation vs. static memory allocation