# Heap and stack memory

Stack memory and heap memory


  • Stack memory area and the heap area:
    General comes memory, refer to a computer's random access memory (the RAM) , all programs running in the inside. It is roughly divided in computer memory as shown below:

  • Stack memory: the program automatically apply to the operating system allocation and recovery, unified distribution thereafter can no longer expand the program starts, determines the recursion depth has an upper limit, but under the C / C ++ general recursive thousands of times is possible , fast speed, use convenient, but the programmer can not control . If the allocation fails, prompt the stack overflow error. Address stack area decreases in the direction of growth, const local variables is also stored in the stack area, the calling function have local variables and other information present in the stack
//测试栈内存
#include <iostream>
int main()
{
    int i = 10; //变量i储存在栈区中
    const int i2 = 20;
    int i3 = 30;
    std::cout << &i << " " << &i2 << " " << &i3 << std::endl;
    return 0;
}

The test output is:

& I3 <I2 & <& I, proof of address is reduced.


  • Heap memory: a programmer to apply to the operating system memory, when the system receives the application process, will traverse a linked list of free memory address is recorded, is greater than the first spatial find application heap space node, then the node remove from the free list node and assign the node to the space program. Dispensing slower, continuous addresses are not easily fragmented. In addition, the application programmer , the programmer must also destroy responsible , otherwise it leads to memory leaks. Using new / malloc allocated space called a heap , global variables stored in the heap, as compared with when the stack is placed in a huge array of applications, which can reduce the risk of the stack on the stack overflow
//测试堆内存和栈内存的区别
#include <iostream>
int main()
{
    int i = 10; //变量i储存在栈区中
    char pc[] = "hello!"; //储存在栈区
    const double cd = 99.2; //储存在栈区
    static long si = 99; //si储存在可读写区,专门用来储存全局变量和静态变量的内存
    int* pi = new int(100); //指针pi指向的内存是在 堆区,专门储存程序运行时分配的内存
    std::cout << &i << " " << &pc << " " << &cd << " " << &si << " " << pi << std::endl;
    delete pi; //需程序员自己释放
    return 0;
}

The test output is:

run multiple times pi will find the address pointed to is not continuous, leaping; and & si is the same, may be stored in the read region; first three variables are stored in the stack area by automatically distribution and destruction.

Carefully memory leaks
1) paired with a malloc, there should be a free. (C ++ corresponding to new and Delete)
2) to make use on the same layer, not in function malloc, free and outside the function. The best use of these two functions on the same call level.
3) malloc allocation of memory must be initialized. After the free pointer must be set to NULL.


  • Application size limit
    • Stack: In Windows, the stack is expanded to the low address data structure is a contiguous area of memory. This means that the maximum capacity of the stack and stack address is a preset reference system, in the WINDOWS, size (typically 1M / 2M) determined by the compiler stack , if the space exceeds the remaining space of the application stack You will be prompted to overflow. Therefore, from a smaller space on the stack obtained .
    • Heap: heap is extended to higher addresses in the data structure is not continuous memory area. This is because the system is a linked list of free memory for storing the address of a discontinuous nature, and the list traversal direction is from low to higher addresses. The heap size is limited by the computer system effective virtual memory. Thus, the space available heap more flexible, is relatively large .

About the difference between heap and stack metaphor:
the difference between heap and stack can refer to a predecessor of the metaphor of view:

Use the stack as we go to a restaurant for dinner, just a la carte (issued application), pay, and eat (use), fed and left, without regard to vegetable, vegetables and other preparation and washing dishes, scrubbing pots, etc. mop-up work, his advantage is quick, but little freedom.

Heap is like a DIY like to eat the food, too much trouble, but more in line with their own tastes, and the large degree of freedom.

Guess you like

Origin www.cnblogs.com/sstealer/p/11331407.html