Heap, stack, static zone, read-only area in memory allocation

Source: Internet
Author: User

The In-memory stack area is at a relatively high address in the direction of the growth of the address, the stack address is growing downward, the stack is allocated local variable space, the heap area is an upward increase in the memory allocated to the programmer application. In addition, static zones are allocated static variables, global variable space, read-only areas are assigned constants and program code space , and some other partitions. For constants, in practice, they are reused, such as variables A and B are assigned to "ABC" and they actually point to the same address.

main.cppintA =0;//Global Initialization Zone    Char*P1;//Global Uninitialized ZoneMain () {intb//Stack    CharS[] ="ABC";//Stack    Char*P2;//Stack    Char*P3 ="123456";//123456\0 in the constant area, p3 on the stack.     Static intc =0;//Global (Static) initialization zoneP1 = (Char*)malloc(Ten);//HeapP2 = (Char*)malloc( -);//Heapp1="123456";//123456\0 in the constant area, the compiler optimizes P1 to the same place as the "123456\0" that P3 points to. }

So the life cycle of the data on the stack is only in the process of running the function, it is released after running, and can no longer be accessed. and

1. Response of the system after application
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 report the exception prompt stack overflow.
Heap: First of all should know that the operating system has a record of the free memory address of the list, when the system receives the application of the program, it will traverse the list, look for the first space is larger than the requested space of the heap node, and then delete the node from the list of idle nodes, and the node space allocated to the program, in addition, , 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.
In other words , the heap will have to do some follow-up work after the application, which will lead to the question of application efficiency .

P1 = (char *)malloc();   // in C, the malloc function p2 = (Char *)new();      // using the new operator in C + + // ****** Note that P1, p2 itself is in the stack. 

2. Comparison of application efficiency

Stacks are automatically assigned by the system,Faster than Fast。 But programmers can't control it.
Heap is the memory allocated by new, typicallyslower, and prone to memory fragmentation, but it is most convenient to use.
3. Restrictions on size of applications
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, canless space to get from the stack
Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is due toA system is a free memory address that is stored with a linked list, which is naturally discontinuous., while the traversal direction of the list is addressed by a low address to a high address.the size of the heap is limited by the valid virtual memory in the computer system。 Generally speaking, under 32-bit system, the heap memory can reach 4G space. Thusthe space for the heap is more flexible and larger
4. Storage content in heaps and stacks
Stack: When a function is called,The first stack is the next instruction after the function call in the main functionThe address of the next executable statement of the function call statement,then the parameters of the function, in most C compilers, the arguments are left-to-right and then local variables in the function. Note that static variables are not in the stack.
   when the function call is finished, 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: The size of a heap is typically stored in a heap at the head of a pile.the concrete contents of the heap are programmer-arranged
5. Comparison of Access efficiency

Use the stack 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 the freedom is small .
The use of the heap is like a DIY dish that you like to eat, relatively slow , but more in line with their own tastes, and great freedom .

6. The difference between global variables, local variables, static global variables, and static local variables is:

    1. Life cycle is different
    2. Different range of effects
    3. Different allocation methods
    1. Global variables have global scope. A global variable can be used for all source files simply by defining it in one source file. Of course, other source files that do not contain global variable definitions need to declare the global variable again with the extern keyword.
    2. Local variables are also only local scope, it is an automatic object (auto), it does not persist during the program run, but only during the execution of the function, the function of the execution of a call after the end of the variable is revoked, the memory occupied by the recovery.
    3. A static local variable has a local scope, which is initialized only once, and since the first initialization until the end of the program runs, it differs from global variables in that global variables are visible to all functions, whereas static local variables are always visible to the body of the function that defines itself.
    4. Static global variables also have global scope, which differs from global variables in that if a program contains multiple files, it acts on the file that defines it and does not work in other files, that is, variables modified by the static keyword have a file scope. This way, even if two different source files define static global variables of the same name, they are also different variables.

From allocating memory space to see: Global variables, static local variables, static global variables are allocated space in the static storage, and local variables allocated space in the stack.

From the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use. so the function of the static descriptor is different in different places.

Heap, stack, static zone, read-only area in memory allocation

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.