Memory Allocation-difference between heap and stack

Source: Internet
Author: User

Address: http://hi.baidu.com/baixuejiyi1111/blog/item/ebf83824ffdab6b34723e8f9.html

1. Data Structure stack and stack
The stack actually has two data structures: heap and stack.

Both heap and stack use data itemsSort by order.

2. Stack and heap in memory allocation

It is necessary to allocate memory here: Generally, the program is stored in ROM or flash and needs to be copied to the memory for execution during runtime. The memory will store different information separately, as shown in (Data Storage in memory:

0xc0000000 kernel virtual memory-used by Kernel

Stack zone -- used when the program is runningStore Local VariablesTo extend the space down.

0x40000000 memory image of the Shared Library
---- The program runs in the region used to allocate malloc and new applications.
Heap zone ---- used to allocate the memory space requested by the programmer

Read/write area (. Data. BSS) -- used to store global and static variables

0408048000 Read-Only zone storage programs and constants

0 unused Zone

The stack area in the memory is in a relatively high address, and the stack grows down.

Allocate local variable space in the stack. The heap area increases upwards to allocate the memory space applied by programmers. In addition, static variables and global variables are allocated to static zones. Read-Only zones are allocated with constants and program code spaces, and other partitions.

4. Classic example:

Main. cpp
Int A = 0; global initialization Zone
Char * P1; uninitialized globally
Main ()
{
Int B; stack
Char s [] = "ABC"; stack
Char * P2; stack
Char * P3 = "123456"; 123456 \ 0 is in the constant zone, and P3 is in the stack.
Static int C = 0; Global (static) initialization Zone
P1 = (char *) malloc (10); heap
P2 = (char *) malloc (20); heap
}

5. Stack and stack

(1). Different application methods and recovery methods
The first difference between stack and stack is that the application method is different:

Stack (stack) is automatically allocated by the system. For example, we define a char A. The system will automatically open up space for it on the stack.

Heap (HEAP) is the space that programmers apply for as needed, such as malloc (10). It opens up ten bytes of space.

Because the space on the stack is automatically allocated and automatically recycled, the life cycle of the data on the stack is only in the process of running the function. After running the function, the data is released and cannot be accessed. The data on the stack can be accessed as long as the programmer does not release space, but the disadvantage is that memory leakage will occur once the programmer forgets to release the data.

(2). system response after application

STACK: as long as the remaining space of the stack exceeds the applied space, the system will provide the program with memory. Otherwise, an exception will be reported, prompting stack overflow.

Heap: First, you should know that the operating system has a linked list that records the idle memory address. When the system receives a program application, it will traverse the linked list to find the first one that is larger than the requested space.
Node, delete the node from the idle node linked list, and allocate the space of the node to the program. In addition, for most systems, the size of the allocation will be recorded at the first address in the memory space,
In the code
The delete statement can correctly release the memory space. In addition, because the size of the heap node is not necessarily equal to the applied size, the system automatically places the excess part in the idle linked list.
That is to say, the heap will do some subsequent work after the application, which will lead to the application efficiency problem.
(3). Comparison of Application Efficiency
STACK: the stack is automatically allocated by the system, and the speed is fast. But programmers cannot control it.

Heap: Memory allocated by new. It is generally slow and prone to memory fragments. However, it is most convenient to use.

(4). Application size limit
STACK:
In Windows, the stack is a data structure extended to a low address and a continuous memory area. This statement means that the top stack address and the maximum stack capacity are pre-defined by the system.
In Windows, the stack size is 2 m (OR 1 m, which is a constant determined during compilation). If the applied space exceeds the remaining space of the stack, overflow will be prompted. Because
Therefore, the space available from the stack is small.
Heap: the heap is a data structure extended to the high address and a non-sequential memory area. This is because the system uses the linked list to store the idle memory address, which is naturally discontinuous, And the traversal direction of the linked list is from the low address to the high address. The heap size is limited by the valid virtual memory in the computer system. It can be seen that the space obtained by the heap is flexible and large.

(5) Storage content in heap and stack
Due to the limited size of the stack, It is physical to use subfunctions, not just logical.

STACK: When a function is called, the first entry to the stack is the address of the next instruction (the next executable statement of the function call statement) after the function is called in the main function, then there are various parameters of the function. In most C compilers, the parameters are from right to left and then the local variables in the function. Note that static variables are not included in the stack.
When the function call ends, the local variable first goes out of the stack, then the parameter, and the top pointer of the stack points to the address of the initial storage, that is, the next instruction in the main function, where the program continues to run.
Heap: Generally, the heap size is stored in one byte in the heap header. The specific content in the heap is arranged by the programmer.

You can also refer to this question for storage content. This question also involves the survival of local variables.

(6) Comparison of access efficiency
Char S1 [] = "aaaaaaaaaaaaa ";
Char * S2 = "bbbbbbbbbbbbbbbbb ";
Aaaaaaaaaaa is assigned a value at the runtime and placed in the stack.
Bbbbbbbbbbbbb is determined during compilation and placed in the heap.
However, in future access, the array on the stack is faster than the string pointed to by the pointer (such as the heap.
For example:
# Include
Void main ()
{
Char A = 1;
Char C [] = "1234567890 ";
Char * P = "1234567890 ";
A = C [1];
A = P [1];
Return;
}
Corresponding assembly code
10: A = C [1];
00401067 8A 4D F1 mov Cl, byte PTR [ebp-0Fh]
0040106a 88 4D FC mov byte PTR [ebp-4], Cl
11: A = P [1];
0040106d 8B 55 EC mov edX, dword ptr [ebp-14h]
00401070 8A 42 01 mov Al, byte PTR [edX + 1]
00401073 88 45 FC mov byte PTR [ebp-4], Al

6. Comparison Between Stack and stack
The difference between stack and stack can be seen from the metaphor of a senior:
Using Stacks is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it). If you are full, you can leave, without having to worry about the preparation work, such as cutting and washing dishes, as well as the cleaning and tail work such as washing dishes and flushing pots, his advantage is fast, but his degree of freedom is small.
Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.