Difference between data structure and memory heap and stack __ data structures

Source: Internet
Author: User
Tags constant

Special statement: This article refers to the blog: http://blog.csdn.net/wolenski/article/details/7951961#comments


Heap and stack in my eyes has been a very vague concept, but simply understood as: the stack is a data structure, is used to store data. Because some of the things that have been studied recently, there are more heaps and stacks involved, has always been in a vague state, so after careful study with a clear and orderly understanding, here to share with you, although there are many explanations on the network but I still like to write down, one can comb their own ideas and have a profound memory, Secondly, according to their own understanding of the way to record the convenience can be consulted later, there is not the right place to ask friends to enlighten, the girl will be grateful.


Stacks and stacks in a data structure

The heap and stack are two different data structures in the structure. Both are data structures in sequential order of data items.

Stack: Like a bucket or box with data

The stack is a kind of data structure that is familiar to everybody, it is one kind has the backward first out of date construction, in other words, the first to store, the first to store the back, which is similar to what we take in the box at the bottom of the things (put in the earlier objects), we first have to remove the pressure on top of the object (into the relatively late object).

Heap: Like an inverted tree

A heap is a sorted tree-type data structure in which each node has a value. Typically, the data structure of a heap is a two-fork tree. The heap is characterized by the minimum (or maximum) value of the root node, and the two trees of the root node are also a heap. Because of this feature of the heap, it is commonly used to implement precedence queues, the access to the heap is random, as we take the book on the library shelves, 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 in front, the bookshelf this mechanism is different from the box, We can take out the books we want directly.

Heap and stack in memory allocation

We are often used is not the data structure of the heap and stack, the reason is that the data structure of the heap and stack is to be said after the heap and stack area to distinguish, please be sure to pay attention.

Here is the C language Program memory allocation in the heap and stack, here it is necessary to put the memory allocation also mention, we do not want to hate this girl Lori long-winded, generally, the program is stored in ROM or flash, the operation needs to be copied into the memory of the execution, memory will be stored separately for different information.

In the memory of the stack area in a relatively high address, with 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. In addition, there are static regions that assign static variables to global variable space. Read-only areas are assigned constants and program code space, as well as some other partitions.


Let's look at a classic example:

Main.cpp

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 in the stack area

static int c = 0; global (static) initialization area

P1 = (char *) malloc (10); heap

P2 = (char *) malloc (20); heap

}

The difference between stack area and heap area in memory allocation

0. Different ways of application and recovery

I don't know if you've got it. The first difference between heap and stack is the difference of application: Stack (English name; stack) is the system that allocates space automatically.

, for example, we define a char A; the system automatically opens up space on the stack. and the Heap (English name: heap) is the programmer according to the need to apply for their own space, such as malloc (10); Opening up is a byte 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.


1, after the application system response

Stack: As long as the remaining space of the stack is larger than the requested 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 is applied to the application, will traverse the list, looking for the first space is larger than the application 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 means the heap will have to do some follow-up work after the application, which leads to an application efficiency problem.


2, the application efficiency comparison

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, the size of the application limit

Stacks: In Windows, stacks are data structures that extend to a low address and are 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 using a linked list to store the free memory address, nature is discontinuous, and the chain of the 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, heap and stack of memory content

Because of the size limit of the stack, the use of a child function is still physical, not just logical meaning.

Stacks: In a function call, the first stack is the address of the next instruction (the startled 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 arguments have a right to left stack, and then a local variable 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.

5, on the heap and stack a comparative image of the metaphor

Stacks: Using stacks like we go to restaurants to eat, just order (send the application), pay, eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables, such as preparation and washing dishes, such as cleaning the pot, his advantage is fast, but the freedom of small.


Heap: Using the heap is like a do-it-yourself like dishes, more trouble, but more in line with their own taste, and freedom of large


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.