The difference between the stack and heap data structures and memory

Stack and heap data structures

You must first know the stack on a data structure, although we call it so, but the stack is actually two data structures: stacks and stacks.

Data structures are heap and stack, a data item in sequence.

Like stack of barrels or boxes loaded data

We start with the more familiar stack of talking about it, it is a kind of data structure LIFO nature, that is to say after the deposit of the first take, after the first deposit is taken.

This is like what we want out on the bottom of the box inside the (relatively early into objects), we must first remove the pressure on it objects (objects into relatively late).

Heap like a tree upside down

  • The stack is different, is a heap sort through the tree data structure, each node has a value.
  • Usually we said data structure heap, refers to binary heap.
  • Characterized heap root value of the minimum (or maximum), and the root node is a stack of two subtrees.

Because of the heap this feature, used to implement priority queues, heap access is free, just as we take books on library shelves, although placing the book is in order, but we want to take any one time do not like the stack, like, first remove all the previous books, bookshelves this mechanism is different from the case, we can remove the book we want to direct.

 

Memory allocation in stack and heap

Look at Baidu Encyclopedia of the memory stack introduction:

Stack space allocation

Stack (OS): released automatically assigned by the operating system, the stored function parameters, local variables, and the like. Operate similarly to a stack data structure.

Stack (operating system): general distribution release by the programmer, if the programmer does not release at the end of the program may be recovered by the OS, it touches on similar distribution list.

Stack buffer mode

Stack using a cache in the storage space when they are usually called, calls completed immediately released.

Heap is stored in the secondary cache, the life cycle is determined by the garbage collection algorithm virtual machine (once orphaned objects can not be recycled). So call these objects is more speed to be relatively lower.

Here to talk about memory allocation in C language program heap and stack memory allocation where it is necessary to also mention, I do not think that my long-winded, under normal circumstances the program stored in the Rom (read-only memory, such as hard disk) or Flash, requires copying RAM (random access memory RAM), runtime execution, the RAM can store different information, as shown below:

Stack memory address region at a relatively high growth-direction address for the words, the address stack grows downwards.

Allocating stack space for local variables, the heap area is increased up to the application programmer memory space allocation. There is also a static area is assigned a static variable, the global variable space; read-only area is assigned constants and program code space; and a number of other partitions.

Look at a very popular online classic example:

main.cpp 

int a = 0; //全局初始化区 
char *p1; //全局未初始化区 
main() 
{ 
    int b; //栈 
    char s[] = "abc"; //栈 
    char *p2; //栈 
    char *p3 = "123456"; //123456\0在常量区,p3在栈上。 
    static int c =0; //全局(静态)初始化区 
    p1 = (char *)malloc(10); //堆 
    p2 = (char *)malloc(20);  //堆 

0. mode of application and recycling in different ways

I do not know if you understand a little bit.

The first difference is that heap and stack different application methods: Stack (English name is stack) is automatically allocated space, for example, we define a char a; the system will automatically open up its space on the stack. The stack (the English name is the heap) is a programmer needs its own space applications, such as malloc (10); open ten bytes.

Because space on the stack is automatically assigned automatic recovery, so the lifetime of the data on the stack just in the process of running the function, run out after the release, can no longer access. The heap of data as long as the programmer does not free up space, you can have access to, but the disadvantage is that you forget the release will be a memory leak. There are some other differences I think Paraphrase what good online friends summarize here:

1. Application of the system response

Stack: As long as the remaining stack space is greater than the application space, the system will provide program memory, otherwise it will report an exception in the stack overflow.

Heap: you should know that the operating system has a list of free memory address of the record, when the system receives the application process, will traverse the linked list, find the first space is greater than the application heap space node, then the node from free node list to delete, and assign the node to the space program, in addition, for most systems, the first will address this memory space allocated record size this time, so, delete the statement in the code the right to release this memory space. Further, since the size of the size of the stack to find the node is not necessarily exactly equal to the application, the system will automatically back into the excess portion of the free list. 

That pile will need to do some follow-up work after applying this will lead to the problem application efficiency.

2. Comparison of the efficiency of application

It is seen from the first 1:00 and 0:00.

Stack: automatically assigned by the system faster. But the programmer can not control.

Heap: It is a new memory allocation, usually more slowly, and prone to memory fragmentation, but it most convenient to use.

3. The application size limit

Stack: Stack in Windows is an extension to the lower address data structure is a contiguous area of ​​memory. This means that the maximum capacity of the stack address and stack predetermined system is good, in WINDOWS, the stack size is 2M (some say 1M, in short, it is a compile-time constants determined), if when space applications exceeds the remaining space on the stack you will be prompted to overflow. Therefore, less space obtained from the stack. 

Heap: heap is extended to the high-address data structure is not continuous memory area. This is because the system is a linked list of free memory for storing the address of a discontinuous nature, and the list traversal direction is from low to higher addresses. The heap size is limited by the computer system effective virtual memory. Thus, the space available heap more flexible, is relatively large.

4. The storage contents of the heap and stack

Due to the limited size of the stack, so a subroutine or physically meaningful, not just the logical sense.

Stack: the function call, the first into the stack is the next instruction following the function call in the main function (executable statement at a function call statement) address and is a function of various parameters, in most C compiler, from right to left argument is pushed onto the stack, and is a function of the local variables. Note that the static variable is not pushed on. 
After the end of this function call, first-out stack of local variables, then the parameters of the last stack pointer points to the beginning address of the memory, i.e. the main function of the next instruction, the program continues to run from this point. 

Heap: one byte is generally stored in the heap size of the head stack. The specific content of the heap programmers arrangements.

The access efficiency of Comparative

char s1[] = "aaaaaaaaaaaaaaa"; 
char *s2 = "bbbbbbbbbbbbbbbbb"; 

aaaaaaaaaaa at run-time assignment; placed on the stack. 
And bbbbbbbbbbb is determined at compile time; on the heap. 
However, after the access, the array on the stack than strings (e.g., stack) pointer points. 

such as: 

#include 
void main() 
{ 
  char a = 1; 
  char c[] = "1234567890"; 
  char *p ="1234567890"; 
  a = c[1]; 
  a = p[1]; 
  return; 
} 

Corresponding assembly code 
10: A = C [. 1]; 
00,401,067. 8A 4D Fl MOV clbyte PTR [EBP-0Fh] 
0040106A 88 4D the FC MOV byte PTR [EBP-. 4] Cl 
. 11: A = P [. 1]; 
0040106D 8B 55 MOV edxdword PTR EC [EBP-14H] 
00.40107 million. 8A MOV albyte PTR 42 is 01 [EDX +. 1] 
00,401,073 88 45 PTR the FC byte MOV [EBP-. 4] Al

Parable about the difference between stack and heap

The difference between heap and stack can refer to a predecessor of the metaphor of view: 

Use the stack as we go to a restaurant for dinner, just a la carte (issued application), pay, and eat (use), fed and left, without regard to vegetable, vegetables and other preparation and washing dishes, scrubbing pots, etc. mop-up work, his advantage is quick, but little freedom. 

Heap is like a DIY like to eat the food, too much trouble, but more in line with their own tastes, and the large degree of freedom. Metaphor is the image, that is very easy to understand, do not know if you're a little harvest.

 

Problem Description

Programming language books often interpret the value types are created on the stack, reference types are created on the heap, but did not explain the essence of what this heap and stack yes. What is the stack, but in the end what they are, where is it (standing on the actual physical memory point of view)?

  1. Under normal circumstances (runtime) controlled by the operating system (OS) running and when it language?
  2. What is their role in the range?
  3. Their size is determined by what?
  4. Which is faster?

Answer a

Stack is a thread of execution set aside memory space. When the function is called, the top of the stack for local variables, and some bookkeeping data reserved blocks. When the function is finished, the block there is no use, could be used again the next time a function call. Usually LIFO stack (LIFO) manner reserved space; therefore nearest reserved block (reserved block) is usually the first to be released. Doing so makes a simple change stack trace; released from the stack block (free block) but only a pointer offset.

Heap (heap) is reserved for dynamic allocation of memory space. And the stack is not the same, there is no fixed pattern of allocation blocks from the heap allocation and re-; you can allocate and release it at any time. Such that the track of which parts of the heap has been allocated and released become extremely complex; many custom heap allocation strategy used to adjust the performance of different modes of use of the stack.

Each thread has a stack, but each application typically only one heap (although the case is the use of multiple different types of memory allocated heap are some exceptions).

Thread stack allocation 1. When a thread is created, the operating system (OS) for each system level (system-level): The direct answer to your question. Typically, the operating system (runtime) to allocate the heap for the application running through the language of the call. 2. Stack attached to the thread, so that when the end of the thread stack is recovered. Heap is usually assigned when the application starts by running, when an application (process) exit is recycled. 3. When a thread is created, set the stack size. When the application starts, and set the heap size, but can be extended when needed (dispenser to the operating system to apply more memory). 4. Stack faster than heap, because it can easily make access pattern allocation and reallocation of memory (pointer / integer only simple arithmetic incremented or decremented), however, the heap allocated and freed more time complex bookkeeping involved. In addition, each byte on the stack are multiplexed frequently means that it may be mapped into the processor cache, so soon (Translator's Note: locality principle).

The second was

Stack:

  1. As the stack and stored in the computer RAM.
  2. Create variables on the stack when it will expand, and will automatically recover.
  3. Compared to heap much faster in terms of allocation on the stack.
  4. Implemented using the stack data structure.
  5. Store local data, return address, is used as parameter.
  6. When too much can cause a stack overflow stack (infinite times (a lot of) recursive calls, or a lot of memory allocation).
  7. Data on the stack can be accessed directly (not necessarily using pointers to access).
  8. If you know you need to allocate the exact size of the data and not much time before the compilation, you can use the stack.
  9. It decided to limit the capacity of the stack when you start the program.

Heap:

  1. And stack as stored in a computer RAM.
  2. Variable must be manually released heap, the scope of the problem does not exist. Available data delete, delete [] or free to release.
  3. Slower compared to allocate memory on the stack.
  4. On-demand through the program.
  5. A lot of allocation and release can cause memory fragmentation.
  6. In C ++, created according to use pointers to access several of the heap, malloc to allocate memory using new or.
  7. If the buffer is too large, then the application, the application may fail.
  8. During the run you do not know how much data or when you need to allocate a lot of memory, it is recommended that you use the heap.
  9. It may cause a memory leak.

For example:

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;

The answer three

Heap and stack are collectively referred to two two kinds of memory allocation. There may be many different implementations, but to achieve to meet a few basic concepts:

1. stack, the new data is added to the stack of items placed on top of other data, you can only remove the top of most data is removed (not offside acquisition). 

2. heap, the position of the data item no fixed order. You can insert and delete any order, because they do not "top" of the concept of data. 

Last picture above good description of the way the heap and stack memory allocation.

Under normal circumstances (runtime) controlled by the operating system (OS) running and when it language?

As previously described, is a heap and stack collectively, you can have many implementations. The computer programs typically have a stack called a call stack is used to store the current function call-related information (for example: the address of the calling function, a local variable), because after the function call needs to return to the calling function. Carry information stack expansion and contraction. In fact, the process is not controlled by the runtime, which consists of a programming language, operating system or even the system architecture to decide.

And dynamic random heap is allocated (memory) collectively in any memory; is unordered. Memory is usually allocated by the operating system, to achieve distribution through the application calls the API interface. There will be some additional overhead in the management of dynamically allocated memory, but this is handled by the operating system.

What is their role in the range?

Call stack is a low-level concept, in terms of procedures, and it "scope" nothing. If you disassemble some code, you'll see a stack pointer references section. On high-level language, the language has its own scope rules. Once the function returns, the local variable in directly immediate release. Your programming language is the basis of this work.

In the heap, it is difficult to define. Scope is defined by the operating system, programming language, but you may increase some of its own rules, to limit the heap in the application range. Architecture and operating system is using virtual addresses, then translated by the processor to the actual physical address, there is a page fault, and so on. They record the pages belonging to that application. But you do not care about these, because you just assigned in your programming language and free memory, and a number of error checking (allocation failure causes of failure and the release of appearance).

Their size is determined by what?

Still, depending on the language, compilers, operating systems and architectures. Stack usually assigned in advance, because the stack must be contiguous block of memory. Language compiler or the OS determine its size. Do not store large blocks of data on the stack, so you can ensure that there is enough space does not overflow, unless the circumstances of the emergence of infinite recursion (the amount of stack overflow), or other unusual programming resolution.

Heap is anything that can be dynamically allocated memory collectively. It depends on how you look at it, and its size is changing. In modern processors and operating systems work is highly abstract, so you do not need to worry about its actual size, unless you have to use the memory you have not been allocated or freed memory under normal circumstances.

Which is faster?

Stack faster because all free memory is continuous, it is not necessary to free memory blocks through the list to maintain. Just a simple pointer to the current top of the stack. Compilers often use a special, fast registers to implement. More important thing is that subsequent operations on the stack are usually concentrated around a block of memory, so a high-speed processor in favor of access (Translator's Note: locality principle).

The answer four

The answer to your question is implementation-dependent, depending on the compiler and processor architectures and different. The following simple explanation:

  1. Stack and heap memory are used to get from the underlying operating system.
  2. In a multi-threaded environment each thread can have his own completely independent stack but they shared heap. Parallel access to be controlled rather than heap stack.

stack:

  1. Stack comprising a linked list to maintain the used and free memory blocks. Newly allocated heap memory is to find a number of blocks from the block of memory to meet the requirements in the idle (with new or malloc). This operation will update block list stack. The meta information is also stored in the heap, the head of each block is often a very small area.
  2. Add a new fast reactors generally extend from the ground to higher addresses. So you can think of the heap as memory allocation and continue to increase in size. If the memory size is small, then the application, the application generally obtained than the memory size to be more from the underlying operating system.
  3. Application and release of many small block may generate the following states: there are many small free blocks between a block. Further applications large memory fails, although the sum of free blocks is sufficient, but idle fragmented pieces, can not meet the size of the application. This is called "heap fragmentation."
  4. When the free block next to the block has been released with new free blocks may be merged with adjacent free blocks into one large free block, this can effectively reduce the production of "heap fragmentation" of. 

Stack:

  1. And sp registers often stack (Translator's Note: "stack pointer", a compilation of understanding friends should know) work together, initially sp points to the top (high stack address).
  2. CPU with the instructions to push data push, pop instruction by popping. When the push by push, sp value is decreased (extended to the low address). When a stack pop shells, sp value increases. Store and retrieve data are CPU registers.
  3. When the function is called, using the CPU specific instruction of the current IP (Translator's Note: "instruction pointer", is a register used to record the location of CPU instructions) are saved. That address code execution. Next, the CPU calls the function address assigned to IP, make the call. When the function returns, the old IP is popped, CPU continues to function codes before calling.
  4. When entering function, sp downwardly extended into the remaining space of sufficient size to ensure that the local variables for the function. If the function has a 32-bit local variable four bytes will leave enough space in the stack. When the function returns, sp returns to the original position by releasing the space.
  5. If the function has parameters, then, before the function call, the argument on the push. Function parameter code to locate and access them through the current position sp.
  6. 函数嵌套调用和使用魔法一样,每一次新调用的函数都会分配函数参数,返回值地址、局部变量空间、嵌套调用的活动记录都要被压入栈中。函数返回时,按照正确方式的撤销。
  7. 栈要受到内存块的限制,不断的函数嵌套/为局部变量分配太多的空间,可能会导致栈溢出。当栈中的内存区域都已经被使用完之后继续向下写(低地址),会触发一个 CPU 异常。这个异常接下会通过语言的运行时转成各种类型的栈溢出异常。(译者注:“不同语言的异常提示不同,因此通过语言运行时来转换”我想他表达的是这个含义)

 

*函数的分配可以用堆来代替栈吗?

不可以的,函数的活动记录(即局部或者自动变量)被分配在栈上, 这样做不但存储了这些变量,而且可以用来嵌套函数的追踪。

堆的管理依赖于运行时环境,C 使用 malloc ,C++ 使用 new ,但是很多语言有垃圾回收机制。

栈是更低层次的特性与处理器架构紧密的结合到一起。当堆不够时可以扩展空间,这不难做到,因为可以有库函数可以调用。但是,扩展栈通常来说是不可能的,因为在栈溢出的时候,执行线程就被操作系统关闭了,这已经太晚了。

转载自:https://blog.csdn.net/langb2014/article/details/79376349

Guess you like

Origin blog.csdn.net/qq_38036909/article/details/82720173