Pile and stack

xiaoxiao2021-03-06  43

Pile and stack

Author: Unknown Date: 2003-7-14 Source: 9CBS

It is generally considered to be divided into these storage regions in C.

1 stack - there is a compiler to automatically distribute release

2 pile - generally distributed by the programmer, if the programmer does not release, the program may be recycled by OS

3 Global zone (static district), global variables and static variables are placed in a piece of, initialized global variables and static variables in one

Regional, uninited global variables and uninitialized static variables are adjacent to another area.

- Procedure end release

4 There is also a specialized place. - Procedure end release

The variables defined in the function body are usually on the stack, and the function assigned by Malloc, Calloc, Realloc, etc. is allocated.

On the heap. In all functions are defined in vitro, it has been stored in the global zone, and no matter where the Static modifier is stored (static)

Zone), static variable defined in all functions in vitro indicates that it is valid in this file, not extern to other files, in the function

The defined Static represents only valid in this function. In addition, "ADGFDF" in the function is placed in a constant area.

such as:

Code:

INT A = 0; // Global Initialization Area

Char * p1; // Globally uninited area

Main ()

{

INT B; / /

Char s [] = "abc"; // stack

Char * p2; // stack

Char * p3 = "123456"; // 123456/0 In the constant region, P3 is on the stack.

Static int C = 0; // Global (static) initialization area

P1 = (char *) malloc (10);

P2 = (char *) malloc (20);

// Allocate the area of ​​10 and 20 bytes in the pile area.

STRCPY (P1, "123456");

// 123456/0 placed in a constant zone, the compiler may optimize "123456" pointed to by P3 into one.

}

There is also a function of a series of reserved fields and delivery parameters when the function is called.

The spatial size of the stack is defined, the default of VC is 2m. The stack is not enough, which is generally allocated in the program, the number of array and recursive functions are too large.

deep. One must know that when a function call returns, it will release all the stack spaces in the function. Stack is used by the compiler

Like, don't worry.

Stacks are dynamically allocated, and you can allocate large memory. But it will create memory leaks with bad.

And frequent Malloc and Free will produce memory fragments (a bit similar to disk fragmentation) because C is allocated to match the dynamic memory.

Memory. And the stack will not produce debris.

Access data on the stack is faster than accessing data on the stack through the pointer.

Generally, the stacks and stacks are the same, that is, the stack is stack, and it is a pile of Heap.

The stack is a first insert, which is generally grown from a high address to a low address.

Reprinted another article:

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

In the book, they are found, 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 the machine

The data structure provided by the system, 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, there is a special message

The depot pointing to the address where the stack is located, there is a dedicated machine instruction to complete the data into the stack out of the stack. This mechanism is characterized by high efficiency, limited data, generally integrated, pointers, floating point numbers, etc., direct support data types,

Other data structures are not directly supported. Because this feature of the stack, the use of the stack is very frequent in the program. Tune

It is done by the direct use stack. The machine's Call instruction implies to push the return address into the stack, then jump into the child of the subroutine address.

Working, the RET instruction in the subroutine is implicitly popped up from the stack and the operation of jump. Automatic variables in C / C are direct benefits

Use the stack example, this is why the function is automatically invalid when the function returns.

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.

The basic Malloc / Realloc / Free function maintained a set of internal heap data structures. When the program uses these functions to get new memory

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

Using system calls to dynamically add the memory size of the program data segment, the newly assigned space first is organized into the internal stack, then

Returns to the caller in an appropriate form. When the program releases the assigned memory space, this memory space is returned in the internal heap structure, possibly

Appropriate processing (for example, with other free spaces into larger idle space), more suitable for the next memory allocation application. This complex

The allocation mechanism is actually equivalent to a memory allocated buffer pool (Cache), using this mechanism has several reasons:

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

Ask (assignment by page); 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 function provided by the system. It is characterized by rapid and efficient, and the disadvantages are limited. The data is not flexible; and the stack is a library

The features provided are characterized by flexible and convenient, and the data adaptation surface is wide, but efficiency has a decrease. Stack is the system data structure, for entering

Cheng / thread is unique; the stack is the internal data structure of the library, not necessarily unique. The memory allocated by different stacks cannot operate with each other. Stack space

Two static allocation and dynamic allocation. Static allocation is the completion of the compiler, such as auto variables (AUTO). Dynamic assignment

Alloca functions are completed. The dynamic assignment of the stack does not need to be released (it is automatically), and there is no release function. Seeing the procedures for portable

Dynamic allocation operations are not encouraged! The allocation of the stack is always dynamic, although all the data spaces will be released back at the end of the program.

System, but accurate application memory / release memory matches the basic elements of a good program.

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

New Post(0)