difference between stack and heap

Difference Between Heap and Stack

 

1. Preliminary knowledge - memory allocation of the program

The memory occupied by the program is divided into the following parts

1. The stack area (stack) - is automatically allocated and released by the compiler, and stores the parameter values ​​of functions, the values ​​of local variables, etc. It operates like a stack in a data structure.

2. The heap area (heap) - generally allocated and released by the programmer. If the programmer does not release it, it may be reclaimed by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list, huh.

3. Global area (static area) (static)—The storage of global variables and static variables is put together, initialized global variables and static variables are in the same area, and uninitialized global variables and uninitialized static variables are in the same area. another adjacent area. Released by the system after the program ends.

4. Literal Constant Area - Constant strings are placed here. Released by the system after the program ends

5. Program code area - store the binary code of the function body.

 

 

example program

//main.cpp

int a = 0; global initialization area

char *p1; global uninitialized area

main(){

int b; stack

char s[] = "abc"; 栈

char *p2; stack

char *p3 = "123456"; 123456\0 is in the constant area, and p3 is on the stack.

static int c = 0; global (static) initialization area

p1 = (char *)malloc(10);

p2 = (char *)malloc(20);

The allocated areas of 10 and 20 bytes are in the heap area.

strcpy(p1, "123456"); 123456\0 is placed in the constant area, the compiler may optimize it into a place with "123456" pointed to by p3.

}

 

 

Second, the theoretical knowledge of heap and stack

2.1 How to apply 

stack: 

Assigned automatically by the system. For example, declare a local variable int b in a function; the system automatically creates space for b in the stack

heap: 

The programmer needs to apply by himself and specify the size, malloc function in c

Such as p1 = (char *)malloc(10);

use new operator in java

Such as p2 = new char[10];

But note that p1, p2 themselves are in the stack.

 

 

2.2 System response 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 an exception will be reported to indicate stack overflow.

Heap: First of all, you should know that the operating system has a linked list that records free memory addresses. When the system receives an application from a program, it will traverse the linked list to find the first heap node whose space is larger than the requested space, and then remove the node from the heap. Delete from the free node list and allocate the space of the node to the program. In addition, for most systems, the size of this allocation will be recorded at the first address in this memory space. In this way, the delete statement in the code In order to correctly release the memory space.

In addition, since the size of the found heap node is not necessarily exactly equal to the size of the application, the system will automatically put the excess part back into the free list.

 

 

2.3 Application size limit 

Stack: Under Windows, the stack is a data structure that extends to a lower address and is a contiguous memory area. This sentence means that the address of the top of the stack and the maximum capacity of the stack are pre-specified by the system. Under WINDOWS, the size of the stack is 2M (some say 1M, in short, it is a constant determined at compile time), if When the requested space exceeds the remaining space of the stack, overflow will be prompted. Therefore, less space can be obtained from the stack.

Heap: The heap is a data structure that extends to high addresses and is a discontinuous area of ​​memory. This is because the system uses a linked list to store free memory addresses, which are naturally discontinuous, and the traversal direction of the linked list is from low addresses to high addresses. The size of the heap is limited by the virtual memory available in the computer system. It can be seen that the space obtained by the heap is more flexible and larger.

 

 

2.4 Comparison of application efficiency: 

The stack is automatically allocated by the system, which is faster. But the programmer is out of control.

The heap is memory allocated by new, which is generally slow and prone to memory fragmentation, but it is the most convenient to use.

In addition, under WINDOWS, the best way is to use VirtualAlloc to allocate memory. He is not on the heap, nor on the stack, but directly reserves a piece of memory in the address space of the process, although it is the most inconvenient to use. But it is fast and the most flexible.

 

 

2.5 Storage contents in the heap and stack 

Stack: When a function is called, the first thing pushed into the stack is the address of the next instruction after the main function (the next executable statement of the function call statement), and then the various parameters of the function. In most C compilers , the parameters are pushed onto the stack from right to left, followed by the local variables in the function. Note that static variables are not pushed onto the stack. When this function call ends, the local variables are popped off the stack first, then the parameters, and finally the top of the stack pointer points to the first stored address, which is the next instruction in the main function, and the program continues to run from this point.

Heap: Generally, one byte is used to store the size of the heap at the head of the heap. The specific 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 runtime;

And bbbbbbbbbbb is determined at compile time;

However, in subsequent accesses, the array on the stack is faster than the string pointed to by the pointer (eg the heap).

for example:

#include

void main()

{

char a = 1;

char c[] = "1234567890";

char *p ="1234567890";

a = c[1];

a = p[1];

return;

}

 

 

2.7 Summary:

The difference between heap and stack can be seen with the following analogy:

Using the stack is like going to a restaurant to eat, just order food (issue an application), pay, and eat (use), leave when you are full, and don’t care about preparatory work such as cutting vegetables, washing vegetables, washing dishes, brushing pots, etc. Finishing work, his advantage is that it is fast, but the degree of freedom is small.

Using the heap is like making your own favorite dishes. It is more troublesome, but it is more in line with your own taste and has a large degree of freedom.

 

See this is well written, you can check it out

https://www.2cto.com/kf/201302/190704.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325609545&siteId=291194637