C ++ memory area

In C ++, the memory is divided into five regions, namely, heap, stack, free store, global / static memory and a constant memory area. The stack size is 1M under Windows

Project -> Properties -> Linker -> System -> stack reserve size can set the stack size

Stack

Is a contiguous area of ​​memory allocated by the compiler when necessary, the storage area is automatically removed when not needed variables. Variables which are typically local variables, functions and other parameters.

stack

Discontinuous memory areas, managed by the linked list, heap size is limited effective virtual memory in the computer system

 

Heap and stack difference:

Management : For the stack is concerned, is managed automatically by the compiler, we do not need to manually control; For a heap, the release is controlled by the programmer work, prone to memory leak.
Space : In general, the 32-bit system, 4G heap memory space can be achieved from this point of view heap memory is almost no limitation. But for the stack is concerned, there is usually a certain amount of space, for example, in VC6 below, the default stack space is 1M (like, can not remember). Of course, we can modify:   
    Open the project, and then click the Action menu as follows: Project-> Setting-> Link, select Output in Category, and then set the maximum and commit stack in the Reserve.
Note: reserve a minimum of 4Byte; commit is retained in the virtual memory page file inside a large stack will make it opens up a larger set of values, may increase the memory overhead and start-up time.
Debris problem : For a heap in terms of frequent new / delete will inevitably lead to discontinuity of memory space, causing a lot of debris, so reducing the efficiency of the procedure. For the stack is concerned, it will not have this problem, because the stack is a last-out queue.
Growth direction : For a heap is concerned, the growth direction is upward, i.e. toward the direction of increasing memory addresses; for the stack in terms of its growth direction is downwards, is reduced in the direction of growth of the memory address.
Distribution : heap is allocated dynamically, not statically allocated heap. There are two kinds of stack allocation: static allocation and dynamic allocation. Static allocation is done compiler, such as the allocation of local variables. Dynamically allocated by alloca allocation function, but dynamic stack allocation and heap are different, his dynamic allocation is released by the compiler, we do not need to manually achieve.
Allocative efficiency: Stack Data Structure is provided a machine system, the computer will provide the underlying support for a stack: a special register storage allocated address stack, the stack has a dedicated push instruction is executed, which determines the efficiency of the stack is relatively high. Stack is the C / C ++ library provided, its mechanism is complicated, for example, in order to allocate a memory library functions will follow a certain algorithm (specific algorithm may reference a data structure / operating system) search heap memory available space of sufficient size, if there is no space of sufficient size (probably due to too many fragmented memory), it is possible to call the system function to increase the memory space program data segment, so there is ample opportunity to be assigned to the size of the memory, and then return. Obviously, heap much lower efficiency than the stack.

Free store

malloc allocation, free release

Global / static storage area

Used to store global variables and static variables,

Constant storage area

Store constants can not be modified

Guess you like

Origin www.cnblogs.com/larry-xia/p/11871007.html