Stack and stack

Source: Internet
Author: User
Stack and stack: from the very beginning of the programming class, the teacher told us that good things are put in the stack, and junk things are put in the heap, which I have never fully understood, later, I did some embedded projects and took some lessons, and I had some understanding.

STACK:It can be divided into heap and stack.

SlaveData StructureStack and stack belong to different data structures. Stack is a special linear structure and can be considered as a special linear table, but they are added and deleted in a special way, this linear table can only be added from the tail (also known as the inbound stack), but also must be deleted from the tail (Out stack or pop-up stack), that is, the so-called advanced post-release. This role will be discussed later. The heap is different. The heap is a special binary tree with a depth of H and meets the following conditions: 1. from the root to the H-1 layer is a complete binary tree; 2. any node is smaller than its descendant, and the smallest element is on the heap root (Wikipedia ).

In terms of data structure, these two types are different,Stack is a linear structure, while stack is a tree structure..

However, in actual operations, we operate on the memory.Memory AngleThe stack is different from the one defined above.

From the perspective of Embedded C language programming, General programs are stored in flash, and operations are performed in Ram during actual operation. Therefore, space needs to be allocated in the memory at the beginning, when allocating space at the beginning, allocate some space to store global variables and allocate a stack. The size of the stack depends on the memory size and allocation policy. The heap is equivalent to the idle area in the back memory except the constant zone, code zone, and stack.

The following code shows where the variables we write are generally written:

# Include "stdafx. H "# include <iostream> using namespace STD; int main () {int A = 0; // A in the stack char B = ''; // B static int C = 0 in the stack; // C in the static zone char * P1 = (char *) malloc (10); free (P1) // return 0 on the heap of P1 ;}

Stack and heap have the following differences in memory:

The first point isDifferences in allocated spaceStack is the space allocated by the system. As long as there is space in the stack, the system will allocate it, but the heap will apply automatically like in the memory;

The second point isRelease space differentlyThe stack is automatically released by the system, and the items in the stack need to be manually released by the programmer. If the stack is not released, the memory space will be occupied all the time. However, the stack allocation space is much smaller. if you write a statement, char a [10000]. Generally, an error is reported during debug-stack overflow. This is not the case in the heap, because the heap is an idle space in the memory. Generally, the system records the idle space in the memory in the form of a linked list, therefore, you do not need to care too much about the memory space;

The third point isEfficiencyAnd can be divided into access efficiency and storage efficiency. In terms of access efficiency, the stack is allocated by the system, and the speed is fast, while the heap access speed is slow. In terms of storage, the memory and cache ing are involved, the heap is generally in the second-level cache. The stack generally uses the first-level cache, and the storage speed on the hardware is different. Then, the actual execution logic of the program is implemented. The heap uses malloc, you need to find idle memory, and the stack can be directly stored without stack overflow.


To sum up, the stack is the most convenient and brainless. It is also precious for others to help you allocate everything (System and compiler) (the space is small and will be recycled immediately in the scope ); heap is much more troublesome than stack. The only advantage is that you can dynamically allocate large space on your own. In this way, as the previous C ++ teacher said, good things are actually put in the stack.

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.