Original address: http://blog.csdn.net/xcyuzhen/article/details/4543264C++Central stack Area constant area (studied by an interview topic) the-Geneva- - +: on#include<iostream.h>voidMain () {Chara[]="ABC"; StackCharb[]="ABC"; StackChar* c="ABC"ABC in the constant area, C on the stack. Char* d="ABC"; The compiler might associate it with a C-pointing"ABC"optimized into one place. Const Chare[]="ABC"; StackConst Charf[]="ABC"; stack cout<< a <<" "<< b <<" "<< C <<" "<<d <<" "<<e <<" "<<f <<Endl;cout<< (a==b?1:0) <<endl<< (C==d?1:0) <<endl<< (e==f?1:0) <<Endl;} The output from the above program is ABC abc abc abc ABC010the following things go from: http://hi.baidu.com/diewty/blog/item/3d2191f4e55fd5e77709d71b.htmlOne by C + +The compiled program consumes memory in the following sections1, Stack (stack)-Automatically allocated by the compiler to release, store parameter values for functions, values for local variables, and so on. It operates in a manner similar to a stack in a data structure. 2, Heap area (heap)-typically released by the programmer, if the programmer does not release, the program may end up being recycled by the OS. Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe. 3, Global zone (static zone) (Static--The storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another adjacent area. -System released after the program is finished4, literal constant area-the constant string is put here. Released by the system after the program is finished5, program code area-binary code that holds the body of the function. Ii. example procedure This is written by a predecessor, very detailed//main.cpp#include<iostream.h>#include<string.h>//#include <malloc.h>//the header file for malloc can be #include<malloc.h> or #include<stdlib.h>#include <stdlib.h>intA =0; global initialization ZoneChar*p1; Global uninitialized area main () {Const Char* m ="123456";intb; StackCharS[] ="ABC"; StackChar*p2; stackChar*P3 ="123456"; 123456 in the constant area, p3 on the stack. Static intc =0; Global (static) initialization zone P1= (Char*) malloc (Ten); P2= (Char*) malloc ( -); Areas that are allocated 10 and 20 bytes are in the heap area. strcpy (P1,"123456"); 123456 is placed in the constant area, the compiler may associate it with the P3 point"123456"optimized into one place. cout<< (M = = P3?1:0) <<endl;//result is 1cout<< (P1 = = P3?1:0) <<endl;//result is 0cout<<p1<<" "<<p3<<" "<<endl;//The result is 123456 123456} ii. theoretical knowledge of heaps and stacks2. 1 Application Method Stack: Automatically assigned by the system. For example, declaring a local variable in a functionintb; The system automatically opens up a space heap for B in the stack: requires the programmer to apply himself, and indicates the size, in C, the malloc function such as P1= (Char*) malloc (Ten); In C++using the new operator such as P2= (Char*) malloc (Ten); But note that P1, p2 itself is in the stack. 2.2post-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 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's space allocated to the program, in addition, The size of this allocation is recorded at the first address in this 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. 2. 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. 2. 4 application Efficiency comparison: Stacks are automatically assigned by the system and are faster. But programmers can't control it. Heap is the memory allocated by new, the general speed is relatively slow, and prone to memory fragmentation, but the most convenient to use. In addition, under Windows, the best way is to use VirtualAlloc to allocate memory, he is not in the heap, nor in the stack is directly in the process's address space to keep a fast memory, although the most inconvenient to use. But the speed is fast, also the most flexible. 2. 5 stacks and stacks of storage content stack: When a function is called, the first stack is the address of the next instruction in the main function (the next executable statement of the function call statement), and then the parameters of the function, in most C compilers, the arguments are left-to-right 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 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 arranged by programmers. 2. 6 Comparison of Access efficiencyCharS1[] ="aaaaaaaaaaaaaaa"; Char*S2 ="bbbbbbbbbbbbbbbbb"AAAAAAAAAAA is assigned at run time, while BBBBBBBBBBB is determined at compile time, but in subsequent accesses, the array on the stack is faster than the string that the pointer points to (for example, a heap). For example: #includevoidMain () {CharA =1; CharC[] ="1234567890"; Char*p ="1234567890"; a= c[1]; A= p[1]; return; } The corresponding assembly codeTen: a = c[1]; 004010678A 4D F1 mov cl,bytePTR [ebp-0Fh] 0040106A the4D FC movbytePTR [ebp-4],CL One: a = p[1]; 0040106D 8B -EC mov edx,dword ptr [ebp-14h]004010708A the onmov al,bytePTR [edx+1] 00401073 the $FC movbytePTR [ebp-4],al First reads the elements in the string directly into the register CL, while the second one reads the pointer values into EDX, which is obviously slow to read the characters according to EdX. 2. 7 Summary: The difference between heap and stack can be seen in the following analogy: the use of stacks like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat 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 less freedom. 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. 1, memory allocation: Heap: Generally released by the programmer, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is different from the heap in the data structure, and the distribution is similar to a linked list. The following keywords may be used:New, malloc, delete, free, and so on. Stack: The compiler (Compiler) automatically allocates the release, storing the function's parameter value, the value of the local variable, and so on. It operates in a manner similar to a stack in a data structure. 2, how to apply: Heap: Requires the programmer to apply and specify the size. In C, the malloc function, such as P1= (Char*) malloc (Ten); in C + +with the new operator, but note that P1, p2 itself is in the stack. Because they can still be considered as local variables. Stack: Automatically assigned by the system. For example, declaring a local variable in a functionintb; The system automatically opens up space for B in the stack. 3, System response: Heap: The operating system has a list of idle memory address, 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's space allocated to the program, in addition, For most systems, the size of this allocation is recorded at the first address in this memory space, so that the DELETE statement in the code can properly free up the memory space. In addition, because the size of the found heap node does not necessarily equal the size of the application, the system will automatically re-put the extra portion into the idle list. 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. 4, size limitation: heap: Is the data structure that extends to the high address and is a discontinuous memory area. 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. 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 address and the maximum capacity of the stack is the system pre-defined, under Windows, the size of the stack is fixed (is a compile-time determination of the constant), if the requested space exceeds the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small. 5, Efficiency: heap: Is the memory allocated by new, the general speed is relatively slow, and prone to memory fragmentation, but the most convenient to use, in addition, under Windows, the best way is to allocate memory with VirtualAlloc, he is not in the heap, Nor is it that the stack is directly retaining a fast memory in the process's address space, although it is most inconvenient to use. But the speed is fast, also the most flexible. Stack: Automatically allocated by the system, faster. But programmers can't control it. 6, storage content: Heap: It is generally the size of a heap in the head of a heap that is stored in a single byte. The concrete contents of the heap are arranged by programmers. Stack: The first stack in a function call is the address of the next instruction in the main function (the next executable statement of the function call statement) and then the parameters of the function, in most C compilers, the arguments are left-to-right and then local variables in the function. Note: 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. 7, Access efficiency: heap:Char*S1 ="Hello Word"is determined at compile time; stack:CharS1[] ="Hello Word"; it is assigned at run time; it is faster to use an array than a pointer, because the pointer needs to be relayed in the EDX register in the underlying assembly, and the array is read directly on the stack. End of Category: compilation principle
C + + Stack area in the heap zone constant area