"Pile" and "Stack"

xiaoxiao2021-03-06  14

Heap and Stack (stack) is C / C programming inevitably two basic concepts that will encounter. First of all, these two concepts can be taught

Find in the book, they are all basic data structures, although the stack is simpler.

In the specific C / C programming framework, these two concepts are not parallel. Research on the underlying machine code can reveal that the stack is a machine system

The data structure provided, and the stack is provided by the C / C function library.

Specifically, modern computers (serial execution mechanisms) are directly supported by the stack in the code underlayer. This is reflected in the specialist

The device points to the address where the stack is located, and there is a dedicated machine instruction to complete the data into the stack out of the stack. This mechanism is characterized by high efficiency and supporting

According to limited, the data type directly supported by integers, pointers, floating point numbers, etc., and other data structures are not directly supported. Because of the stack

This feature is very frequent on the programs in the program. The call to the subroutine is done directly by the stack. Machine CALL

The Token implicitly pushed the return address into the stack, then jumps to the operation of the subroutine address, and the RET instruction in the subroutine is hidden from the stack.

The operation returns the address and jumps. Automatic variables in C / C are examples of direct use stacks, which is why when the function returns,

The reason for the automatic variable of this function is automatically invalid (because the stack restores the state before the call is called).

Unlike the stack, the data structure of the heap is not supported by the system (whether it is a machine system or operating system), but is provided by the function library. base

This Malloc / Realloc / Free function maintains a set of internal heap data structures. When the program uses these functions to get new memory space

When the function first tries to find the available memory space from the internal heap. If there is no memory space you can use, try to utilize the system.

The call to dynamically add the memory size of the program data segment, and the newly assigned space first is organized into the internal stack, and then appropriate shape

Returns to the caller. When the program releases the allocated memory space, this memory space is returned in the internal heap structure, which may be appropriate.

(, For example, with other free spaces into larger idle spaces), more suitable for the next memory allocation application. This complex allocation mechanism is real

As a cache, which is equivalent to a memory allocation, using this mechanism has the following reasons:

1. System calls may not support any size memory allocation. Some systematic system calls only support fixed size and their multiple memory requests

(Press page allocation); this kind of waste will cause waste to a large amount of small memory classification.

2. System call requesting memory may be expensive. System calls may involve transformation of user-state and core states.

3. Memory allocation without management is easy to cause memory fragmentation under the distribution of complex memory.

Pile and stack comparison

From the above knowledge, the stack is the functionality provided by the system. It is characterized by rapid and efficient, disadvantages are limited, and the data is not flexible; and the stack is a function library

The function is characterized by flexible, and the data adaptation surface is wide, but the efficiency has a decrease. Stack is the system data structure, for process / line

The process is unique; the stack is the internal data structure in the library, not necessarily unique. The memory allocated by different stacks cannot operate with each other. Stack space division

Both the allocation and dynamic allocation. Static allocation is the completion of the compiler, such as auto variables (AUTO). Dynamic allocation is finished by the alloca function

to make. The dynamic assignment of the stack does not need to be released (it is automatically), and there is no release function. Seeing that the dynamic allocation operation of the stack is

Not encouraged! The allocation of the spatial space is always dynamic. Although all data spaces will be released back to the system at the end of the program, but precise

Applying for memory / release memory matches is a basic element of a good program.

转载请注明原文地址:https://www.9cbs.com/read-50851.html

New Post(0)