The difference between heaps and stacks (articles that have been turned over countless times)

Source: Internet
Author: User

I. Preliminary knowledge-memory allocation of the program
The memory used by a program compiled by C + + is divided into the following sections
1. Stack (stack)-Automatically allocated by the compiler to release, store the function parameter value, local variable value and so on. Its
Operations are similar to stacks in data structures.
2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the end of the program may be the OS back
Inbox Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe.
3, Global Zone (Static)-, the storage of global variables and static variables is placed in a block, initialized
Global variables and static variables in an area, uninitialized global variables and uninitialized static variables in adjacent
An area. -released by the system after the program is finished.
4, literal constant area-the constant string is put here. Released by the system after the program is finished
5. Program code area-binary code that holds the function body.


Ii. Examples of procedures
It was written by a predecessor, very detailed
Main.cpp
int a = 0; Global initialization Zone
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 zone
P1 = (char *) malloc (10);
P2 = (char *) malloc (20);
Areas that are allocated 10 and 20 bytes are in the heap area.
strcpy (P1, "123456"); 123456/0 in the constant area, the compiler may associate it with the "123456" that P3 points to
Optimized into one place.
}


Ii. theoretical knowledge of heaps and stacks
2.1 How to apply
Stack
Automatically assigned by the system.   For example, declare a local variable int b in the function; The system automatically opens up empty for B in the stack.
Room
Heap
Requires the programmer to apply himself and indicate the size of the malloc function in C
such as P1 = (char *) malloc (10);
Using the new operator in C + +
such as P2 = new CHAR[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 requested space, the system will provide memory for the program, otherwise it will report the exception prompt stack Overflow
Out
Heap: First you should know that the operating system has a list of idle memory addresses, when the system receives the application of the program,
The linked list is traversed, looking for the first heap node that is larger than the requested space, and then linking the node from the Idle node list.
and allocates the space of that node to the program, and, for most systems, in this memory space
The size of this allocation is recorded at the first address so that the DELETE statement in the code can properly free up the memory space.
In addition, because the size of the found heap node does not necessarily exactly equal the size of the request, the system will automatically
Into the list of idle links.

2.3 Application Size Limits
Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. The meaning of this sentence
Think is the top of the stack address and the maximum capacity is the system pre-defined, under Windows, the size of the stack is 2M (there are
1M, which is a constant at compile time), if the requested space exceeds the stack's remaining space, the
Hint overflow. Therefore, the space available from the stack is small.
Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list
The free memory address is, naturally, discontinuous, while the traversal direction of the list is addressed by the low address to the high address. The size of the heap
Limited by the virtual memory available in the computer system. Thus, the space of the heap is more flexible and relatively large.



2.4 Comparison of application efficiency:
The stack is automatically assigned by the system and is faster. But programmers can't control it.
Heap is the memory allocated by new, the general speed is relatively slow, and prone to memory fragmentation, but the most convenient to use.
In addition, under Windows, the best way is to allocate memory with VirtualAlloc, he is not in the heap, nor is the stack
Preserves a chunk of memory directly in the address space of the process, although it is most inconvenient to use. But the speed is fast, also the most flexible.


2.5 Storage contents in stacks and stacks
Stack: When a function is called, the first stack is the next instruction in the main function (the next bar of the function call statement can be
The address of the execution statement, and then the parameters of the function, in most C compilers, the arguments are left-to-right into the stack
, and then a local variable in the function. Note that static variables are not in the stack.
When the function call is finished, the local variable is first out of the stack, then the parameter, and the last stack pointer points to the first saved
The next instruction in the main function, and the program continues to run from that point.
Heap: The size of a heap is typically stored in a heap at the head of a pile. The concrete contents of the heap are arranged by the programmer.

2.6 Comparison of access efficiency

Char s1[] = "AAAAAAAAAAAAAAA";
Char *s2 = "BBBBBBBBBBBBBBBBB";
AAAAAAAAAAA is assigned at run time;
And BBBBBBBBBBB is determined at compile time;
However, in subsequent accesses, the array on the stack is faster than the string that the pointer points to (for example, a heap).
Like what:
#include
void Main ()
{
char a = 1;
Char c[] = "1234567890";
Char *p = "1234567890";
A = c[1];
A = p[1];
Return
}
The corresponding assembly code
10:a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0fh]
0040106A 4D FC mov byte ptr [ebp-4],cl
11:a = p[1];
0040106D 8B-EC mov edx,dword ptr [ebp-14h]
00401070 8A mov al,byte ptr [edx+1]
00401073 FC mov byte ptr [ebp-4],al
The first reads the elements in the string directly into the register CL, and the second one reads the pointer value to
In edx, it is obviously slower to read the characters according to EdX.


2.7 Summary:
The difference between heap and stack can be seen in the following analogy:
Using stacks like we eat in a restaurant, just order (apply), pay, and eat (use), eat full
Go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but since
Small by degrees.
Using the heap is like making your own favorite dishes, more troublesome, but more in line with their own tastes, and free
The high degree. Classic )

The difference between heaps and stacks (articles that have been turned over countless times)

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.