What is heap? What is stack?

Source: Internet
Author: User
1. English name

Heap and stack are two basic concepts frequently encountered in C/C ++ programming. Let's take a look at their English representation:

Heap

Stack-stack

2. Understanding from two levels: data structure and system

In a specific C/C ++ programming framework, these two concepts are not parallel. After in-depth research at the Assembly level, we will find that the stack is the data structure provided by the machine system, and the stack is provided by the C/C ++ function library. These two concepts can be understood at the data structure and system layers:

1. From the perspective of Data Structure, stack is an advanced linear table. As long as the linear table conforms to the advanced and later principles, it is a stack. It is irrelevant whether the storage method (implementation method) is sequential storage (sequential stack) or chained storage (chain stack. Heap is a binary tree with the maximum heap minimum heap. The sorting algorithm has common heap sorting.

2. At the system level, stack is an advanced storage area allocated by the system for running programs. When learning bootloader, we know that the heap stack should be allocated for each working mode in the initialization phase after power-on. The stack here actually refers to the stack. The stack statement is only for historical reasons. When executing a function, the storage unit of local variables within the function can be created on the stack (for the CISC architecture, the storage unit of local variables is created on the Register in the RISC architecture ), these storage units are automatically released when function execution ends. Heap is a global storage space that can be used by programs managed by the system. dynamic memory allocation is done from the heap.

Specifically, computers (serial execution mechanisms) now support the stack data structure at the code hierarchy. This is reflected in the fact that there are dedicated registers pointing to the address of the stack, and dedicated machine commands to complete the operations of data in and out of the stack. For example, worker FD and ldmfd in the ARM command. Because the stack memory allocation operation is built into the processor's instruction set, the efficiency is very high, but the supported data is limited. Generally, it is a data type directly supported by integer, pointer, floating point, and other systems, other data structures are not directly supported. In CISC, the stack is used to call subprograms. The automatic variables in C/C ++ are also examples of directly using stacks, which is why when the function returns, the cause of the function's automatic variable failure (because the stack restores the status before the call ). All of these are done through registers in the same stage. These are detailed in the second part of the Summary.

Unlike the stack, the stack data structure is not supported by the system (whether a machine system or an operating system), but provided by the function library. The basic malloc/free function maintains an internal heap data structure. When the program uses these functions to obtain new memory space, this function first tries to find available memory space from the internal heap. If there is no available memory space, we try to use the system call to dynamically increase the memory size of the program data segment. The newly allocated space is first organized into the internal heap, and then return it to the caller in the appropriate form. When the program releases the allocated memory space, this memory space is returned to the internal storage Heap Structure and may be processed properly (for example, merged into a larger free space with other free space ), it is more suitable for the next memory allocation application. This complex allocation mechanism is actually equivalent to a buffer pool (cache) for memory allocation. The reasons for using this mechanism are as follows:

1. system calls may not support memory allocation of any size. Some system calls only support fixed memory requests and their multiples (allocated by PAGE), which will cause waste for a large number of small memory categories.

2. The system call request memory may be expensive because it may involve switching between the user State and the core state.

3. Unmanaged memory allocation can easily cause memory fragmentation when a large amount of complex memory is allocated and released.

Summary:

1. Stack is a function provided by the system. It features fast and efficient, but its disadvantage is that there are limits and data is not flexible. Stack is a function provided by the function library, which features flexibility and convenience, data is widely adapted, but the efficiency is reduced.

2. the stack is the system data structure and is unique to processes/Threads. The heap is the internal data structure of the function library, which is not necessarily unique. The memory allocated by different stacks cannot operate on each other.

3. Stack space is divided into static space allocation and dynamic space allocation. Static allocation is completed by the compiler, such as automatic variable allocation. The alloc function completes dynamic allocation. The stack does not need to be released dynamically (automatically), so there is no release function. For the sake of portable programs, dynamic stack allocation is not encouraged! Heap space allocation is always dynamic. Although all data spaces are released back to the system at the end of the program, precise memory application/release matching is the basic element of a good program.

3. Further refined analysis at the system level

Next we will repeat the stack and stack at the system level to better understand these two concepts.

1. Application Method

Stack: automatically assigned by the system.

Heap: the size specified by the programmer.

2. system response after application

Stack: as long as the remaining space of the stack is greater than the requested 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 idle memory addresses. When the system receives a program application, it traverses the linked list to find the heap node with the first space greater than the requested space, 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, so that the delete statement in the code 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.

3. Application size limit

Stack: In ARM, stack is pre-configured. In start. S, there can be four different stacks: full stacks increase progressively, full stacks decrease, empty stacks increase progressively, and empty stacks decrease progressively. Commonly used is full stack decline, that is, downward (low address) Direction of growth. This is easy to understand if you analyze the stack settings of bootloader.

Heap: The data structure extended to the high address, which is 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.

4. Application Efficiency Comparison

Stack: automatically assigned by the system, which is faster. But programmers cannot control it.

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

5. Storage content

Stack: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by local variables in the function. Note that static variables are not included in the stack. When the function call ends, the local variables first go out of the stack, then the parameters, and the top pointer of the stack points to the starting address, that is, the next instruction in the main function, the program continues to run at this point.

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.

6. Comparison of access efficiency

Stack: assign values at runtime to ensure high access efficiency.

Heap: It is determined during compilation that the access efficiency is not as high as stack.

Because the stack is allocated by the system, more specifically, this is closely related to the CPU architecture of the CPU or the CISC architecture. In the two different instruction set architectures, the processing of memory allocation is obviously different. In a balanced architecture, one principle is to minimize access to memory, instead of register. Therefore, the number of registers is significantly higher than that of the CISC architecture. The heap is provided by the function library, and the relationship with the system is far away.

++ ++

It can be simply understood:

Heap: the location of the space allocated by functions such as malloc. The address increases from low to high.

Stack: it is the space used for automatic Variable Allocation and function calling. The address is reduced from high to low.

Prerequisites-program memory allocation

The memory occupied by a c/C ++ compiled program is divided into the following parts:

1. stack: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation method is similar to the stack in the data structure.

2. heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled 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.

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

4. Text Constant Area-constant strings are placed here. The program is released by the System

5. program code area-stores the binary code of the function body.

Ii. Example Program

This is written by a senior. It is very detailed.

// 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 is in the constant zone, and p3 is on the stack.

Static int c = 0; Global (static) initialization Zone

P1 = (char *) malloc (10 );

P2 = (char *) malloc (20 );

The allocated 10-byte and 20-byte areas are in the heap area.

Strcpy (p1, "123456"); 123456 is placed in the constant area, and the compiler may optimize it to the "123456" that p3 points.

}

Ii. Theoretical knowledge of heap and stack

2.1 Application Method

Stack:

Automatically assigned by the system. For example, declare a local variable int B in the function; the system automatically opens up space for B in the stack.

Heap:

The programmer needs to apply and specify the size. In c, the malloc Function

For example, p1 = (char *) malloc (10 );

Use the new operator in C ++

For example, p2 = (char *) malloc (10 );

But note that p1 and p2 are in the stack.

2.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 idle memory addresses. When the system receives a program application,

The linked list is traversed to find the heap node with the first space greater than the requested space. Then, the node is deleted from the idle node linked list and allocated to the program, for most systems, the size of the allocation will be recorded at the first address in the memory space, so that the delete statement in the code 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.

2.3 Application size limit

STACK: in Windows, a stack is a data structure extended to a low address and a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. In WINDOWS, the stack size is 2 MB (OR 1 MB, in short, it is a constant determined during compilation. If the requested space exceeds the remaining space of the stack, overflow will be prompted. Because of this, 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.

2.4 comparison of application efficiency:

The stack is automatically allocated by the system, which is faster. But programmers cannot control it.

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

In addition, in WINDOWS, the best way is to use VirtualAlloc to allocate memory. Instead of heap or stack, it is to reserve a fast memory in the address space of the process, although it is the most inconvenient to use. But the speed is also the most flexible

Storage content in 2.5 heap and stack

STACK: when calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement in the function call statement), and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by 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.

2.6 comparison of access efficiency

Char s1 [] = "aaaaaaaaaaaaa ";

Char * s2 = "bbbbbbbbbbbbbbbbb ";

Aaaaaaaaaaa is assigned a value at the runtime;

Bbbbbbbbbbbbb is determined during compilation;

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

The first type reads the elements in the string directly into the cl register, while the second type reads the characters in edx and then reads the characters according to edx, which is obviously slow.

2.7 summary:

The difference between stack and stack can be seen in the following metaphor:

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.

The differences between stack and stack are as follows:

The heap and stack of the operating system, as mentioned above, will not be mentioned much.

There is also the heap and stack in the data structure. These are different concepts. Here, the heap actually refers to a Data Structure (meeting the heap nature) of the priority queue. The 1st elements have the highest priority; stack is actually a mathematical or data structure that meets the needs of the advanced and later stages.

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.