[Switch] stack and stack differences

Source: Internet
Author: User

Complete parsing of heap and stack in C ++

Memory Allocation:

Heap: the operating system has a linked list that records the idle memory address. 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. We often talk about memory leakage. The most common is heap leakage (and resource leakage). It refers to the leakage of programs in operation. If the program is closed, the operating system will help release the leaked memory.

STACK: When a function is called, the address of the next instruction (the next executable statement of the function call statement) in the first main function into the stack is 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.


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.


In some cases, 3, 4 are combined, and 3 is divided into free storage (malloc/free) and Global/static storage.

This is related to the compiler and the operating system.


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 // correction: ABC is allocated to the static storage area, not the stack.

Char * P2; stack

Char * P3 = "123456"; 123456 \ 0 is in the constant zone, and P3 is in 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 into a place with the "123456" that P3 points.


Ii. Theoretical knowledge of heap and stack

2.1 Application Method


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.


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.


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. In this way, 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 size of the application, 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 of the main function (the next executable statement of 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:

# I nclude

Void main ()


Char A = 1;

Char C [] = "1234567890 ";

Char * P = "1234567890 ";

A = C [1];

A = P [1];



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.


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.

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


2.8 additional knowledge:

Heap and stack are two basic concepts that C/C ++ programming will inevitably encounter. First, both concepts can be found in the data structure book. They are both basic data structures, although the stack is simpler.

In a specific C/C ++ programming framework, these two concepts are not parallel. The Research on the underlying machine code reveals that the stack is the data structure provided by the machine system, while the stack is provided by the C/C ++ function library.

Specifically, modern computers (serial execution mechanisms) directly support the stack data structure at the bottom of the Code. 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. This mechanism is characterized by high efficiency and limited data types supported by systems such as integers, pointers, and floating point numbers. It does not directly support other data structures. Due to the characteristics of stack, the use of stack is very frequent in the program. The call to the subroutine is done directly using the stack. The call command of the machine implicitly pushes the return address into the stack, and then jumps to the subprogram address, the RET command in the sub-program implicitly pops up the return address and redirects from the stack. The automatic variables in C/C ++ use the stack directly, which is why when the function returns, the reason for the automatic change of the function to automatically expire (because the stack resumes the status before the call ).

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/realloc/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, the system calls are used to dynamically increase the memory size of the program data segment. The newly allocated space is first organized into the internal heap and then returned to the caller in an appropriate form. When the program releases the allocated memory space, this memory space is returned to the internal Heap Structure and may be processed properly (for example, merged into a larger idle space with other idle 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. There are several reasons for using this mechanism:

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). This will cause a waste for a large number of small memory categories.

2. System Call memory application may be expensive. System calls may involve switching between user and core states.

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

Stack and stack comparison

From the above knowledge, stack is a function provided by the system, featuring fast and efficient, with limitations and inflexible data. Stack is a function provided by the function library, featuring flexibility and convenience, data is widely adapted, but the efficiency is reduced. The stack is the system data structure, which is unique for processes/Threads. The heap is the internal data structure of the function library, which is not necessarily unique. The memory allocated by different heaps cannot be operated logically. Stack space is divided into static allocation and dynamic allocation. Static allocation is completed by the compiler, such as automatic variable allocation. Dynamic Allocation is completed by the alloca function. 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.

What is the difference between stack and stack?

The main differences are as follows:

1. Different management methods;

2. Different space sizes;

3. Whether fragments can be generated is different;

4. Different Growth directions;

5. Different allocation methods;

6. Different Allocation Efficiency;

Management Method: For stacks, it is automatically managed by the compiler without manual control. For heaps, the release work is controlled by programmers and memory leak is easily generated.

Space size: Generally, in a 32-bit system, the heap memory can reach 4 GB. From this perspective, there is almost no limit on the heap memory. But for the stack, there is usually a certain amount of space. For example, under vc6, the default stack space is 1 MB (as if so, I cannot remember ). Of course, we can modify:

Open the project and choose Project> setting> link, select output from category, and set the maximum value and commit of the stack in reserve.

Note: The minimum reserve value is 4 byte. Commit is retained in the page file of the virtual memory. Compared with the general setting, commit makes the stack open up a large value, memory overhead and startup time may be increased.

Fragmentation problem: for the heap, frequent New/delete operations will inevitably lead to memory space disconnections, resulting in a large number of fragments, reducing program efficiency. For the stack, this problem will not exist, because the stack is an advanced and outgoing queue. They are so one-to-one correspondence that it is impossible to have a memory block popped up from the middle of the stack, before the pop-up, the post-stack content has been popped up. For details, refer to the data structure. We will not discuss it one by one here.

Growth direction: For the stack, the growth direction is upward, that is, the direction to the memory address increase; For the stack, the growth direction is downward, is to increase towards memory address reduction.

Allocation Method: The heap is dynamically allocated without static allocation. There are two stack allocation methods: static allocation and dynamic allocation. Static allocation is completed by the compiler, such as local variable allocation. Dynamic Allocation is implemented by the alloca function, but the stack dynamic allocation is different from the heap dynamic allocation. Its Dynamic Allocation is released by the compiler without manual implementation.

Allocation Efficiency: the stack is the data structure provided by the machine system, and the computer will provide support for the stack at the underlying layer: allocate a dedicated register to store the stack address, the output stack of the Pressure Stack has dedicated Command Execution, which determines the high efficiency of the stack. The heap is provided by the C/C ++ function library, and its mechanism is very complicated. For example, to allocate a piece of memory, library functions search for available space in heap memory based on certain algorithms (for specific algorithms, refer to data structures/operating systems, if there is not enough space (probably because there are too many memory fragments), it is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to allocate enough memory, then return. Obviously, the heap efficiency is much lower than the stack efficiency.

From this point, we can see that compared with the stack, the use of a large number of new/delete operations may easily cause a large amount of memory fragments; because of the absence of dedicated system support, the efficiency is very low; because it may lead to switching between the user State and the core state, the memory application will become more expensive. Therefore, stacks are the most widely used in applications. Even function calls are completed using stacks. The parameters and return addresses in the function call process are as follows, both EBP and local variables are stored in stacks. Therefore, we recommend that you use stacks instead of stacks.

Although the stack has so many advantages, but because it is not so flexible as the heap, sometimes it is better to allocate a large amount of memory space.

Whether it is a heap or a stack, it is necessary to prevent cross-border phenomena (unless you intentionally cross-border it), because the cross-border result is either a program crash, either it is to destroy the heap and stack structure of the program and generate unexpected results. It is considered that the above problems did not occur during your program running. You should be careful, maybe it will collapse at some time. At that time, debugging was quite difficult :)

By the way, there is another thing. If someone puts the stack together, it means stack, not heap, huh, huh, clear?

What is stack?

In msdn, the thread stack size is described as follows:

Platform SDK: DLLs, processes, and threads

Thread stack size

Each new thread has es its own stack space, consisting of both committed and reserved memory. The system will commit one page blocks from the reserved stack memory as needed, until the stack cannot grow any farther.

The default size for committed and reserved memory is specified in the executable file header. the default reserved memory is one megabyte. to specify a different default stack size, use the stacksize statement in the module definition (. def) file. your linker may also support a command-line option for setting the stack size. for more information, see the documentation provided with your linker.

Threads that call the C run-time library or the Windows API must allow sufficient stack space for use of these functions. Do not lower the reserved stack size below 64 KB.

To increase the amount of stack space which is to be initially committed for a thread, specify the value in the dwstacksize parameter of the createthread or createremotethread function. this value is rounded to the nearest page. the call to create the thread fails if there is not enough memory to commit or reserve the number of bytes requested. if dwstacksize is smaller than the default reserve size, the new thread uses the default reserve size. if dwstacksize is larger than the default reserve size, the reserve size is rounded up to the nearest multiple of 1 MB.

Windows Server 2003 and Windows XP: If the dwcreationflags parameter of createthread or createremotethread is disabled, the dwstacksize parameter specifies the amount of stack space which is to be initially reserved for the thread.

The stack is freed when the thread terminates.








(To) http://blog.csdn.net/jsjwql/archive/2007/09/10/1779516.aspx

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.