Getting started with the C language stack-the difference between heaps and stacks

Source: Internet
Author: User

Take a look at a classic example that is popular online

1//MAIN.C:
2 intA =0;//global Initialization Zone3 Char*p1;//global uninitialized zone4 Main ()5 {6    intb;//Stack7    CharS[] ="ABC";//Stack8    Char*p2;//Stack9    Char*P3 ="123456"; //123456in the constant area, p3 on the stack. Ten    Static intc =0;//Global (Static) initialization zone OneP1 = (Char*)malloc(Ten); Heap AP2 = (Char*)malloc( -); Heap -}

0. Different application methods and recycling methods

I do not know if you have a little understanding, the first difference between heap and stack is the application method is different: Stack (English name is stack) is the system automatically allocated space, 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 for, such as malloc (10), and 10 bytes of space to open. Since the space on the stack is automatically collected automatically, the life cycle of the data on the stack is only run in the function, it is released after running , and can not be accessed again .。 and the data on the heap can be accessed as long as the programmer does not free up space, but the drawback is that once you forget to release it will cause a memory leak. There are some other differences I think the online friends summed up well here to report: 1. Post-application response stack: As long as the remaining space on the stack is larger than the requested space, the system will provide memory for the program, otherwise the exception prompt stack overflow. Heap: The first thing you should know is that the operating system has a linked list that records the free memory address, and when the system receives the application, it iterates through the list, looking for the first heap that is larger than the requested space.     node, then removes the node from the list of idle nodes and assigns the node's space to the program, and for most systems, the size of this allocation is recorded at the first address in the memory space, so that the DELETE statement in the code can properly free up the memory space. Also, because the size of the found heap node does not necessarily equal the size of the request, the system automatically re-places the extra portion into the idle list.      That is, the heap will have to do some follow-up work after the application, which will lead to the question of application efficiency. 2. The comparison of application efficiency is based on No. 0点 and 1th. Stack: Automatically allocated by the system, faster. But programmers can't control it. Heap: Is the memory allocated by new, the general speed is slow, and prone to memory fragmentation, but the most convenient to use. 3. Application size limit stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.   Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large. 4. Storage content in heaps and stacks due to the limited size of the stack, the use of sub-functions has physical meaning, not just logical meaning. Stack: When the 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, and then the parameters of the function, in most C compilers, the arguments are in the right-to-left stack, followed by the local variables in the function. Note that static variables are not in the stack.      At the end of this function call, the local variable is first out of the stack, then the parameter, and the last stack pointer points to the first saved address, which is the next instruction in the main function, and the program continues to run from that point.   Heap: GeneralIs the size of the heap at the head of the heap that is stored in a single byte. The concrete contents of the heap are arranged by programmers. You can also refer to this question for storage content. This problem also involves the survival period of local variables. 5. Comparison of access efficiency   char s1[] = "chars array"; char *s2 = "pointer to string"; An array of chars arrays is assigned at run time, placed on the stack, released after running, and not brought into the main program after running the subroutine . The string pointer pointer to string is determined at compile time, placed in the heap, required to be released manually by the programmer , and the main program can still be accessed after the subroutine is run . However, in subsequent accesses, the The array on the stack is faster than the string that the pointer points to (for example, a heap).  Like what:
1#include <stdio.h>2 3 voidMain ()4 {5     CharA =1;6     CharC[] ="1234567890";7     Char*p ="1234567890";8A = c[1];9A = p[1];Ten     return; One}

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 [eb P-4],alSummaryThe difference between heap and stack can be quoted by a predecessor analogy to see: the use of stacks like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pots and other finishing work, his advantage is fast, but small The use of the heap is like a DIY dish that you like to eat, more trouble, but more in line with their own tastes, and great freedom. turn from: Ultra-super Boy https://www.cnblogs.com/jycboy/p/5178129.html

Getting started with the C language stack-the difference between heaps and stacks

Related Article

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.