Stack and stack differences (memory and data structure) __ data structure

Source: Internet
Author: User
Tags data structures

In the computer field, the stack is a concept that can not be ignored, we write the C language program is basically used. But for a lot of beginners, the stack is a very vague concept. Stack: A data structure, a place to store when the program is running, which may be the knowledge of many beginners, because I used to think so and the word stack in assembly language confused. Some of my friends around the program and the Internet to see a lot of friends are also not sure of the stack, so I think it is necessary to share my views on the stack, there is said the wrong place to invite friends, this will be a great help for everyone to learn.
stacks and heaps of data structuresFirst on the data structure to know the stack, although we call it so, but actually the stack is two kinds of data structure: heap and stack.

Heaps and stacks are data structures that are ordered by data items.

Stacks are like buckets or boxes of data.
We start from the more familiar stack, it is a backward first out of the nature of the data structure, that is, after the storage of the first to take, the first storage. It's like we're going to take out what's underneath the box (the older object we put in), and we're going to first move the object that's pressed on it (the late object).

Piled like a tree upside down.
And the heap is different, the heap is a sort of tree-shaped data structure, each node has a value. Typically, the data structure of the heap refers to a two-fork heap. The heap is characterized by the minimum (or maximum) value of the root node, and the two subtree of the root node is also a heap. Because of this feature of the heap, it is commonly used to implement precedence queues, and the access of the heap is arbitrary, this is like we are in the library of the bookshelf to take the book, although the book is placed in order, but we want to take any one without the same as the stack, first remove all the books, the bookshelf This mechanism is different from the box, we can directly take out the book we want.

stacks and heaps in memory allocationHowever, I would like to say that the focus is not here, I would say that the heap and stack is not a data structure of the heap and stack, the reason to say that the data structure of the heap and stack is for and after I want to say that the heap area and stack area to distinguish, please be sure to pay attention

Let's talk about the heap and stack in the C language Program memory allocation, here it is necessary to mention the memory allocation, we do not think I long-winded, in general, the program stored in ROM or flash, the runtime needs to be copied to the memory of the execution, memory will be stored in different information, as shown in the following figure:

In the memory of the stack area in the relatively high address to the growth direction of the address, the stack address is growing downward.

The stack area is an upward-growing memory space allocated to the programmer for allocating the local variable space. There are also static regions that allocate static variables, global variable spaces, and read-only areas that assign constants and program code space, as well as some other partitions.

Take a look at a classic example of a popular online:

int a = 0; Global initialization Area
Char *p1; Global uninitialized Zone
Main ()
int b; Stack
Char s[] = "ABC"; Stack
Char *p2; Stack
Char *p3 = "123456"; 123456\0 in the constant area, p3 on the stack.
static int c = 0; global (static) initialization area
P1 = (char *) malloc (10); Heap
P2 = (char *) malloc (20); Heap

0. Different ways of applying and recycling
I don't know if you've got it. The first difference between heap and stack is that the application is different: stacks (English name is stack) is the system allocates space automatically, for example we define a char A; the system automatically opens up space on the stack. The heap (the English name is heap) is the space that the programmer applies to himself, such as malloc (10), and opens up 10 bytes of space. Because the space on the stack is automatically recycled, so the data on the stack life cycle is only in the function of the running process, after the release, can not be accessed. The data on the heap can be accessed as long as the programmer does not release the space, but the disadvantage is that once the release is forgotten it will result in a memory leak. There are some other differences I think the online friends summed up the good here to repeat:

1. Response of the system after application

Stack: As long as the remaining space of the stack is larger than the application space, the system will provide memory for the program, otherwise it will be reported abnormal stack overflow.

Heap: First you should know that the operating system has a record of the free memory address of the list, when the system received the application of the program, will traverse the list, looking for the first space is larger than the requested space heap.

node, and then remove the node from the list of free nodes. The space of the node is assigned to the program, and for most systems, the size of this assignment is recorded at the first address in the memory space, so that the DELETE statement in the code can properly release the memory space. In addition, because the size of the found heap node does not necessarily equal the size of the application, the system automatically puts the extra part back into the free list.
That is, the heap will have to do some follow-up work after the application, which leads to an application efficiency problem.

2. Comparison of application efficiency
According to the No. 0点 and 1th.

Stack: Automatically distributed by the system, faster. But programmers are out of control.

Heap: Is the memory allocated by new, the general speed is slower, and easy to produce memory fragments, but the most convenient to use.

3. Application size limit stack: In Windows, the stack is to the low address extension of the data structure, is a contiguous area of memory. The address of the top of the stack and the maximum capacity of the stack are predetermined by the system, in Windows, the size of the stack is 2M (also some say 1M, in short, a compile-time constant), if the application space over the stack of remaining space, will prompt overflow. Therefore, the space can be obtained from the stack is small.
Heap: The heap is a data structure that is extended to a high address and is a contiguous area of memory. This is because the system is used to store the free memory address of the list, nature is discontinuous, and the link list of the traversal direction is from the low address to the high address. The size of the heap is limited by the virtual memory available in the computer system. This shows that the heap to obtain a more flexible space, but also relatively large.

4. Storage content in heaps and stacks
Because of the limited size of the stack, the use of a child function is also physical, not just logical meaning.

Stacks: When a function is called, the first stack is the address of the next instruction (the next executable statement of the function call statement) after the function call in the main function. Then there are the parameters of the function, in most C compilers, the parameters are pushed from right to left, and then the local variables in the function. Note that static variables are not in the stack.
When the function call is finished, the local variable first goes out of the stack, then the argument, and the last stack pointer points to the address that was first saved, the next instruction in the main function, where the program continues to run.
Heap: The size of the heap is usually stored in a byte at the head of the heap. The specifics of the heap are arranged by the programmer.

You can also refer to this topic for storage content. This problem also involves the survival period of local variables.

5. Comparison of access efficiency char s1[] = "AAAAAAAAAAAAAAA";
The AAAAAAAAAAA is assigned at run time and placed on the stack.
The BBBBBBBBBBB is determined at compile time, and placed in the heap.
However, in future accesses, the array on the stack is faster than the string that the pointer points to (for example, a heap).
Like what:
void Main ()
char a = 1;
Char c[] = "1234567890";
Char *p = "1234567890";
A = c[1];
A = p[1];
The corresponding assembly code
10:a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0fh]
0040106A 4D FC mov byte ptr [ebp-4],cl
11:a = p[1];
0040106D 8B EC mov edx,dword ptr [ebp-14h]
00401070 8A mov al,byte ptr [edx+1]
00401073 FC mov-byte ptr [ebp-4],al

A metaphor for the difference between heap and stack
The difference between stacks and stacks can be seen by quoting the analogy of a predecessor:
Use stacks just like we eat in restaurants, just order (apply), pay, and eat (use), go after full, do not have to pay attention to cutting vegetables, vegetables, such as preparation and washing dishes, such as cleaning the pot, his advantage is fast, but the degree of freedom is small.

The use of the heap is like a do-it-yourself like to eat dishes, more trouble, but more in line with their own taste, and greater freedom. Metaphor is very image, said very easy to understand, do not know if you have a little harvest.


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: 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.