C + + Memory storage Area Division
Interview written Test frequently
In memory, the storage of data is not disorganized, but it has the corresponding partition, according to the data type to be placed in the corresponding location.
The storage area is from the highest storage address to the lowest storage address, in turn:
- Command line parameter area: command line parameters and environment variables;
- stack: refers to the compiler when needed, the storage area of the variable is automatically cleared when it is not needed. The variables inside are usually the function's parameter values, local variable equivalents, which operate in a way similar to stacks in data structures.
- Heap area: Dynamically allocating memory. Usually released by the programmer, if the programmer does not release, at the end of the program, the operating system may be automatically recycled. It is not the same as the heap of data structures, and is distributed in a similar way to linked lists.
- Data segment:
Global Zone ( static zone ): 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, after the end of the program is released by the system. literal constant area: The constant string is put here. Released by the system after the program is finished.
- Code snippet: Binary code that holds the body of the function
Main.cpp Program code Area int a = 0; Global initialization zone char *P1; Global uninitialized Zone main () { int b;//Stack char s[] = "abc";//Stack char *p2;//Stack char *p3 = "123456";//123456\0 in constant area, p3 On the stack. static int c = 0;//global (Static) initialization area P1 = (char *) malloc (); P2 = (char *) malloc (20); Areas that are allocated 10 and 20 bytes are in the heap area. strcpy (P1, "123456"),//123456\0 is placed in a constant area, and the compiler may optimize it with the "123456" that P3 points to as a place. }
Reference Links:http://my.oschina.net/hnuweiwei/blog/224346 and the Java memory allocation is great! http://developer.51cto.com/art/201104/255220.htm Very detailed!
Partitioning of C + + memory storage areas