Heap memory (HEAP) and stack memory (stack) detailed

Source: Internet
Author: User
Tags data structures

Original address: http://blog.csdn.net/abcjennifer/article/details/39780819


Heap: Sequential random stack: Advanced out


The difference between heap and stack one, preparatory knowledge-memory allocation of programs
The memory used by a program compiled by C + + is divided into the following sections
1, stack area (stack)-by the compiler automatically assigned to release, store the function of the parameter values, local variables and other values. It works like a stack in a data structure
2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the program at the end may be reclaimed by the OS. Note that it is different from the heap in the data structure, and the distribution is similar to the list
3, Global area (static)--------------------------------------------------------------- -System release after the program is finished
4, literal constant area-the constant string is here. Released by system after program is finished

5, program code area-the binary code that holds the function body.


Second, the example procedure
This is written by a predecessor, very detailed

main.cpp  
int a = 0; global initialization area 
char *p1; global uninitialized Zone 
main () 
{     
     int b; stack 
     char s[] = "abc"; stack  
     char *p2; Stack  
     char *p3 = "123456"; 123456\0 in the constant area, p3 on the stack.  
     static int c = 0; global (static) initialization area  
     P1 = (char *) malloc;  
     P2 = (char *) malloc;  
     Areas that are allocated 10 and 20 bytes are in the heap area.  
     strcpy (P1, "123456"); 123456\0 in a constant area, the compiler might optimize it to a place with the "123456" that P3 points to.  
}  
Theory knowledge of heap and stack
2.1 Application Methods
Stack: Automatically allocated by the system. For example, declare a local variable int b in a function; The system automatically opens up space for B in the stack

Heap: Require the programmer to apply and indicate the size,

In c malloc functions such as P1 = (char *) malloc (10);

In C + +, use the new operator such as P2 = (char *) malloc (10), but note that P1, p2 itself is in the stack


2.2 Response of the system after application

Stack: As long as the remaining space of the stack is larger than the application space, the system will provide memory for the program, otherwise it will be reported abnormal stack overflow. Heap: You should first know that the operating system has a linked list that records free memory addresses, when the system receives the application, it traverses the list, looks for the first heap node that is larger than the requested space, deletes the node from the Free node list, and assigns the node space to the program, and for most systems, The size of this assignment is recorded at the first address in this memory space, so that the DELETE statement in the code can properly release the memory space. In addition, because the size of the found heap node does not necessarily equal the size of the application, the system automatically puts the extra part back into the free list.

Note here that the malloc allocation failure returns a null pointer, but a new allocation failure throws only an exception and requires

catch (const bad_alloc& e) {

return-1;

}
2.3 Limit of application size
Stacks: In Windows, stacks are data structures that extend to a low address and are a contiguous area of memory. This sentence means that the top of the stack address and the maximum capacity of the stack is the system in advance, in Windows, by the compiler to determine the size of the stack (General 1m/2m), if the application space over the stack of the remaining space, will prompt overflow. Therefore, the space can be obtained from the stack is small.
Heap: The heap is a data structure that is extended to a high address and is a contiguous area of memory. This is because the system is used to store the free memory address of the list, nature is discontinuous, and the link list of the traversal direction is from the low address to the high address. The size of the heap is limited by the virtual memory available in the computer system. This shows that the heap to obtain a more flexible space, but also relatively large.


2.4 Application Efficiency comparison: The stack is automatically allocated by the system, faster. But programmers are out of control.

A heap is a memory that is allocated by new, typically slower, and prone to memory fragmentation, but is most convenient to use. In addition, in Windows, the best way is to allocate memory with VirtualAlloc, he is not in the heap, nor in the stack is directly in the process of the address space to keep a fast memory, although the most inconvenient to use. But fast, and most flexible.


2.5 stacks and stacks of stored content
Stacks: When a function is called, the first stack is the address of the next instruction in the main function (the next executable statement of the function call statement). Then there are the parameters of the function, in most C compilers, the parameters are pushed from right to left, and then the local variables in the function. Note that static variables are not in the stack. When the function call is finished, the local variable first goes out of the stack, then the argument, and the last stack pointer points to the address that was first saved, the next instruction in the main function, where the program continues to run.

Heap: The size of the heap is usually stored in a byte at the head of the heap. The specifics of the heap are arranged by the programmer.


2.6 Comparison of access efficiency

[CPP]View plain copy char s1[] = "AAAAAAAAAAAAAAA";  In stack char *s2 = "BBBBBBBBBBBBBBBBB"; Static variable stack aaaaaaaaaaa is assigned at run time, while BBBBBBBBBBB is determined at compile time, but in future access, the array on the stack is faster than the string (for example, heap) that the pointer points to.
Like what:
[CPP]View plain copy #include void main () {char a = 1;              Char c[] = "1234567890";             Char *p = "1234567890";              A = c[1];              A = p[1];         Return }
The corresponding assembly code
[CPP]  View plain copy 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]    

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.