What are stacks and stacks? Where are they?

Source: Internet
Author: User

 Original English:What and where are the stacks and heap?

  Problem description

In programming language books, it is often explained that the value type is created on the stack, and the reference type is created on the stack, but it does not essentially explain what the stack and stack are. I have only experience in advanced language programming and have not read a clearer explanation of this. I mean, I understand what stacks are, but what exactly they are and where they are (from the perspective of the physical memory of the computer )?

  1. Is it usually controlled by the operating system (OS) and language runtime?
  2. What is the scope of their functions?
  3. What determines their size?
  4. Which is faster?
Answer 1

Stack is the memory space reserved for execution threads. When a function is called, the top of the stack is a partial variable and some bookkeeping data reserved blocks. When the function execution is complete, the block is useless and may be used again in the next function call. The stack usually uses the post-in-first-out (LIFO) method to reserve space. Therefore, the latest reserved block is usually released first. This can make the trace stack easier. Releasing a block from the stack is nothing more than a pointer offset.

Heap is a reserved memory space for dynamic allocation. Unlike stack, there is no fixed mode for allocating and re-allocating blocks from the stack; you can allocate and release it at any time. This makes it complicated to track which heap has been allocated and released. Many custom heap allocation policies are used to adjust the heap performance in different usage modes.

Every thread has a stack, but every application usually has only one heap (even if multiple heap is used to allocate memory for different types ).

Answer your question directly: 1. When a thread is created, the operating system (OS) allocates a stack for each system-level thread. Generally, the operating system allocates a heap for the application by calling the runtime of the language. 2. the stack is attached to the thread, so the stack is recycled when the thread ends. The heap is usually allocated when the application is started through running, and is recycled when the application (process) exits. 3. Set the stack size when the thread is created. When the application starts, set the heap size, but you can expand it as needed (the distributor requests more memory from the operating system ). 4. the stack is faster than the heap because its access mode enables it to easily allocate and re-allocate memory (pointers/Integers only perform simple increment or decrease operations ), however, when the heap is allocated and released, more complicated bookkeeping is involved. In addition, the frequent reuse of each byte on the stack means that it may be mapped to the processor cache, so it is very fast (Note: Local principle ).

Answer 2


  1. It is stored in computer RAM like a heap.
  2. Variables created on the stack are extended and automatically recycled.
  3. Stack allocation is much faster than stack allocation.
  4. Use the stack implementation in the data structure.
  5. Stores local data and returns the address, which is passed as a parameter.
  6. When too many stacks are used, Stack Overflow (infinite (massive) recursive calls, or a large amount of memory allocation) may occur ).
  7. Data on the stack can be accessed directly (instead of using pointers ).
  8. Stack can be used if you know exactly the size of the data to be allocated before compilation and it is not too large.
  9. When your program starts, the upper limit of stack capacity is determined.


  1. It is stored in computer RAM like a stack.
  2. The variables on the stack must be manually released, so there is no scope issue. Data can be released using Delete, delete [], or free.
  3. It is slower than allocating memory on the stack.
  4. Distribute as needed through programs.
  5. A large amount of allocation and release can cause memory fragmentation.
  6. In C ++, data created on the stack is accessed using pointers, and memory is allocated using new or malloc.
  7. If the requested buffer is too large, the application may fail.
  8. We recommend that you use the heap when you do not know how much data will be required during running or when you need to allocate a large amount of memory.
  9. Memory leakage may occur.


int foo(){    char *pBuffer; //<--nothing allocated yet (excluding the pointer itself, which is allocated here on the stack).    bool b = true; // Allocated on the stack.    if(b)    {        //Create 500 bytes on the stack        char buffer[500];        //Create 500 bytes on the heap        pBuffer = new char[500];    }//<-- buffer is deallocated here, pBuffer is not}//<--- oops there‘s a memory leak, I should have called delete[] pBuffer;
Answer 3

Heap and stack are two general terms for memory allocation. There may be many different implementation methods, but the implementation must comply with several basic concepts:

1. For the stack, the newly added data items in the stack are placed on the top of other data. You can only remove the data at the top of the stack (you cannot obtain the data by offside ).

2. For the heap, there is no fixed sequence of data item positions. You can insert and delete data in any order because they do not have the "TOP" Data concept.

The figure above shows the heap and stack memory allocation methods.

Is it usually controlled by the operating system (OS) and language runtime?

As mentioned above, heap and stack are collectively referred to as there are many implementation methods. A computer program usually has a stack called the call stack, which is used to store information related to the current function call (for example, the address of the main function and the local variable), because the function needs to be returned to the main function after being called. Stack carries information through expansion and contraction. In fact, the program is not controlled by the runtime, but determined by the programming language, operating system, or even system architecture.

Heap is dynamically and randomly allocated (memory) in any memory, that is, unordered. The memory is usually allocated by the operating system and allocated by calling the API through the application. There will be some additional overhead in managing the Dynamic Allocation of memory, but this is handled by the operating system.

What is the scope of their functions?

The call stack is a low-level concept. As far as the program is concerned, it has nothing to do with the "scope of action. If you disassemble some code, you will see the pointer reference stack section. For advanced languages, languages have their own range rules. Once the function returns, the local variables in the function are directly released. Your programming language is based on this work.

It is also difficult to define in the heap. The scope of action is limited by the operating system, but your programming language may add its own rules to limit the scope of the heap in the application. The architecture and operating system use virtual addresses, which are then translated from the processor to the actual physical address, and there are page errors. They record the page that belongs to the application. But you don't need to worry about this, because you only allocate and release the memory in your programming language, and some error checks (Reasons for allocation failure and release failure ).

What determines their size?

Still, dependent on language, compiler, operating system and architecture. Stack is usually allocated in advance, because the stack must be a continuous memory block. The compiler or operating system of a language determines its size. Do not store large pieces of data on the stack. This ensures that there is sufficient space to avoid overflow, unless infinite recursion occurs (amount, Stack Overflow) or other uncommon programming resolutions.

Heap is a collective term for any memory that can be dynamically allocated. It depends on what you think of it, and its size changes. The operating system and the Modern processor work in a highly abstract way, so you do not need to worry about the actual size under normal circumstances, unless you have to use the memory you have not allocated or the memory has been released.

Which one is faster?

Stack is faster because all idle memory is continuous, so you do not need to maintain idle memory blocks through the list. It's just a simple pointer to the top of the current stack. The compiler usually uses a dedicated and fast register. More importantly, subsequent stack operations are usually concentrated near a memory block, which facilitates high-speed access to the processor (Note: Local principle ).

Answer 4

The answer to your question depends on implementation, and varies with the compiler and processor architecture. The following is a simple explanation:

  1. Both stack and stack are used to obtain memory from the underlying operating system.
  2. In a multi-threaded environment, each thread can have its own independent stack, but they share the heap. Parallel access is controlled by the heap instead of the stack.


  1. The heap contains a linked list to maintain used and idle memory blocks. The newly allocated memory (with new or malloc) on the heap is to find some suitable blocks from idle memory blocks that meet the requirements. This operation updates the block linked list in the heap. These metadata is also stored on the stack, and is often located in a small area in the header of each block.
  2. The increase of heap is usually extended from the local address to the high address. Therefore, you can consider that the heap size increases with memory allocation. If the applied memory size is small, it usually gets more memory than the applied memory size from the underlying operating system.
  3. Applying for and Releasing many small blocks may produce the following status: there are many small idle blocks between used blocks. An error occurred while applying for a large block of memory. Although the total number of idle blocks is sufficient, the idle blocks are scattered and cannot meet the applied size ,. This is called "heap fragmentation ".
  4. When the used blocks of idle blocks are released next to them, the new idle blocks may be merged with adjacent idle blocks into a large idle block, this effectively reduces the generation of "heap fragments.


  1. The stack often works with the SP register (Translator's note: "Stack pointer", and anyone familiar with the Assembly should know). At first, the SP points to the top of the stack (the High address of the stack ).
  2. The CPU uses the push command to press the data stack, and uses the pop command to play the stack. When pushing the stack, the SP value is reduced (extended to the low address ). When the pop stack is used, the SP value increases. The value of the CPU register is used to store and obtain data.
  3. When a function is called, the CPU uses a specific instruction to press the stack of the current IP address (the Translator's note: "Instruction Pointer", which is a register used to record the location of the CPU instruction. That is, the address of the code to be executed. The CPU then assigns the call function address to the IP address for calling. When the function returns, the old IP address is pushed to the stack, and the CPU continues to remove the code before the function is called.
  4. When entering the function, SP expands down to ensure that sufficient space is reserved for the local variables of the function. If the function has a 32-bit local variable, it will leave enough space in the stack. When the function returns, SP releases space by returning the original position.
  5. If a function has parameters, the parameter is pushed to the stack before the function is called. The code in the function uses the current position of SP to locate parameters and access them.
  6. Function nested calling is the same as magic. function parameters are assigned to each newly called function. The return value address, local variable space, and nested call activity records are pushed into the stack. When the function returns, it is revoked in the correct way.
  7. The stack is subject to memory block restrictions. Constant function nesting/allocating too much space for local variables may cause stack overflow. When the memory area in the stack has been used up, it will trigger a CPU exception. This exception will be converted to stack overflow exceptions of various types through the language runtime. (Note: "exceptions in different languages are different, so they are converted through language runtime." I think he expressed this meaning)

* Can stack be used for function allocation?

No, function activity records (I .e. local or automatic variables) are allocated to the stack. This not only stores these variables, but also can be used for tracking nested functions.

Heap management depends on the runtime environment. c uses malloc and C ++ uses new, but many languages have garbage collection mechanisms.

Stack is a closer combination of lower-level features and the processor architecture. It is not difficult to expand the space when the heap is insufficient because database functions can be called. However, the extended stack is generally impossible, because when the stack overflows, the execution thread is shut down by the operating system, which is too late.

Translator's note

This post on Stack has gained a lot for me. I have read some materials before, and I often think about it when I write my own code. In this regard, he has also discussed many times with Xiangzi (my university roommate, now a graduate of Beijing Post and Telecommunications Institute and technical expert. But after all, it is a knowledge point. After reading this post, the knowledge point is finally connected to a network. This kind of feeling is understandable, and the excitement in the period is self-evident.

There are many followers in this post. I chose the top four. There are also repeated ideas between the four. I personally love the fourth responder. When I saw it, it was an instant climax? However, some concepts such as assembly language, operating system, and computer composition principles are required to know what the registers are, the assembly line instruction mechanism of the computer, and the protection/recovery field are required. All three replies involve virtual memory in the operating system. When comparing the speed, you must have a concept in mind about the "Locality Principle" and the computer high-speed cache.

If you understand this article, I believe you will not only get the heap and stack, but you will understand more!

Excited, there are several points to emphasize. There is no word-by-word translation for translation, most of which are obtained through my personal knowledge accumulation and speculation on the recipient's intention. Please do not chew on words or give them one by one. Our purpose is technical communication, isn't it? This is enough.

The following are some uncertainties:

  1. I have never heard of bookkeeping data, so I have not translated it. In terms of context, can it be thought of as a register value? Function parameters? Return address? If you have any questions, please contact us.
  2. Stack and stack are the same thing. The English expression is stack and heap.
  3. This is the first time I have heard of the concept of calling stacks. You can check the information and study it.

The above is for you. This article is complete.



What are stacks and stacks? Where are they? (Transfer)

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.