Base point @ What's the heap and stack?

Source: Internet
Author: User

[Stack things]
In computer science, a stack is a linear table that is only inserted or deleted at the end of a table.

Stack is a data structure. It stores data based on the principle of first-in-first-out, first-in data is pushed to the bottom of the stack, and finally the data is placed at the top of the stack, data is popped up from the top of the stack when data needs to be read (the last data is read by the first one ).
Stack is a special linear table that can only be inserted and deleted at one end. Pile up items in a bucket, first pile in under pressure, then one piece to pile up. Only one of the above items can be taken when you take the data. The heap and fetch operations are carried out on the top, and the bottom is usually not moving.
Stack is a data structure similar to bucket accumulation items. One End for deletion and insertion is called the stack top, and the other is called the stack bottom. Insert is generally called push and delete is called pop ). Stack is also known as a post-import, first-out table (LIFO table ).
1. Push Algorithm
① If top is greater than or equal to N, overflow information is provided and an error is handled (check whether the stack is full before entering the stack, overflow when full; if not, perform ② );
② Set Top = Top + 1 (the stack pointer plus 1 points to the stack address );
③ S (top) = x, end (X is the element of the new stack );
2. Stack rollback (POP) Algorithm
① If top is less than or equal to 0, the underflow information is provided and an error is handled. (check whether the stack is empty before stack rollback. If it is empty, the underflow occurs. If it is not empty, perform ② );
② X = S (SOP), (The elements after stack rollback are assigned to X );
③ Top = Top-1, end (Stack pointer minus 1, pointing to the top of stack ).

Stack can be used to store breakpoints during function calls and stack is used for recursion!

 

[What is the heap]
In a program, the heap is used to dynamically allocate and release objects used by the program. Call the heap operation in the following cases:

1. Do not know the number and size of objects required by the program in advance.

2. The object is too large to use the stack distributor.

The heap uses the memory allocated to parts other than the code and stack during runtime.

Traditionally, the operating system and Runtime Library are accompanied by a heap implementation. When a process starts, the operating system creates a default heap called a process heap. If no other heap is used, process heap is used to allocate blocks. The Language Runtime Library can also create a separate heap in a process. (For example, the C Runtime Library creates its own heap .) In addition to these dedicated heaps, applications or one of many loaded Dynamic Link Libraries (DLL) can also be created and used separately. Win32 provides a rich set of APIS for creating and using private stacks. For an excellent tutorial on heap functions, see the SDK node on the msdn platform.

When an application or DLL creates a dedicated heap, these heaps reside in the process space and are accessible within the process range. Any data allocated to a given heap should be released from the same heap. (Allocating from one heap and releasing it to another is meaningless .)

In all virtual memory systems, the heap is located on the Virtual Memory Manager of the operating system. The Language Runtime heap also resides on the virtual memory. In some cases, these stacks are on the upper layer of the Operating System Heap, but the language runtime heap manages its memory by allocating large blocks. Using Virtual Memory functions without Operating System Heap allows the heap to allocate and use blocks better.

A typical heap implementation consists of a front-end distributor and a backend distributor. The front-end distributor maintains a free list of fixed size blocks. When the heap receives the allocation call, it tries to find the free block from the front-end list. If this operation fails, the heap will be forced to allocate a large block from the backend (Reserve and submit the virtual memory) to satisfy the request. The general implementation has the overhead of each block allocation, which takes the execution cycle and reduces the available storage zone.

The implementation of Windows NT (Windows NT version 4.0 and later) uses a Free List of 127 8-byte alignment blocks ranging from 8 to 1,024 bytes and a hybrid list. The hybrid list (Free List [0]) contains blocks larger than 1,024 bytes. The Free List contains objects linked together in a two-way link table. By default, the process heap performs the merge operation. (Merge operations combine adjacent free blocks to generate larger blocks .) The merge operation takes an additional period, but reduces the internal fragmentation of the heap block.

A single global lock prevents multiple threads from using heap at the same time. This lock is mainly used to protect the heap data structure from arbitrary access by multiple threads. When the heap operation is too frequent, this lock will negatively affect the performance.

Stack
In the computer field, stack is a concept that cannot be ignored, but many people, even computer professionals, do not know that stack is actually two data structures.

Key points:

Heap: random order

STACK: Advanced and later

Stack and stack differences

I. 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/0 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/0 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. Therefore, 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. However, it is fast and 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 pointer value into EDX. Reading the characters based on edX is obviously slow.

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.

Although the stack is called a connection, they still have a lot of difference. The connection is only due to historical reasons.

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.