c ++ on the difference between the heap and stack

 

In C ++, memory is divided into five areas, they are the heap, stack, free store, global / static storage area and the constant storage area.  
     Stack, who is assigned when needed by the compiler, automatically clear when not needed variable 
storage area. Variables which are typically local variables, functions and other parameters.  
     Heap, who is a block of memory allocated by new, never mind their release compiler, should be by our 
applications to control, a new general would correspond to a delete. If a programmer is not freed, 
then at the end of the program, the operating system will automatically recover.  
     Free store, are those allocated by malloc and other memory blocks, and he is very similar to the heap, 
but it is free to end their lives.  
     Global / static memory, global and static variables are allocated to the same memory, the previous 
C language, is divided into global variables initialized and uninitialized (global and static variables initialized variable 
amount in a region, uninitialized global variables and static variables in a region adjacent to another, while not 
initialized object store can be accessed and manipulated through the void *, released after the end of the program by the system itself 
release), there is no distinction in the C ++ which together, they occupy the same memory area.  
     Constant storage area, which is a special storage area inside the store they are constant, does not allow 
modification (of course, you can also change through non-legitimate means, and the many ways) 

 

Heap and stack What is the difference?   
    The main difference from the following:  
    1, different management;  
    2, spaces of different sizes;  
    3, can produce different fragments;  

    4, different growth directions;  

    5, a different distribution;  

    6, different distribution efficiency;  
    management: For the stack is concerned, is managed automatically by the compiler, we do not need to manually control; For a heap 
, the work is released by the programmer control, prone to memory leak.  
    Space: In general, 32-bit system, 4G heap memory space can be achieved, from this angle 
of view of heap memory is almost no limitation. But for the stack is concerned, there is usually a certain space 
size, 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, in Category 
select Output, 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, set it 
facing a large stack will open up a larger value 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, we will not have this problem, because 
as the stack is advanced out of the queue, they are so one to one that will never have an internal 
memory block pop-up from the middle of the stack, in the pop-up before he in the stack contents above him backward has been ejected details 
can refer to the data structure, here we are not going to discuss it.  
    Growth direction: For a heap is concerned, the growth direction is upward, i.e. toward the side of increased memory address 
To; for the stack in terms of its growth direction is downwards, it 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 
state 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 
to be released compiler, we do not need to manually achieve.  
    Dispensing efficiency: the stack is a data structure of the machine provided by the system, the computer will provide underlying support for the stack: 
allocating a dedicated address register stores the stack, the stack has a dedicated push instruction is executed, which determines the stack 
efficiency is relatively high . Stack is the C / C ++ library provided, its mechanism is complicated, for example, to points 
with a memory, the library functions will follow a certain algorithm (specific algorithm may reference a data structure / operating 
system) in the heap memory the search space of sufficient size available, if there is no space of sufficient size (probably 
due to memory too fragmented), it is possible to call the system function to increase the memory space program data segment, this 
kind have the opportunity to be assigned enough memory sizes and then return. Obviously, heap much lower efficiency than the stack.  
    We can see from here, heap and stack compared to the use of a large number of new / delete is easily made 
into a large number of memory fragmentation; because there is no special support system is inefficient; due may lead to the user mode 
switch and core states memory applications, the cost becomes more expensive. So the stack is the most widely used in the program 
pan, even the called function also use the stack to complete, during the function call parameters, return address, 
EBP, and local variables are used to store a stack of way. Therefore, we recommend you try to use the stack rather than 
the heap.  
    Although the stack has so many benefits, but because it is not as flexible and heap compared sometimes to allocate large 
amounts of memory space, or with a heap better.  

 

 

reference:

https://blog.csdn.net/qianyayun19921028/article/details/80364964

 

Guess you like

Origin www.cnblogs.com/sea-stream/p/11361476.html