1. First, tell the difference between heap heap and stack stack:
A program that has been compiled by C + + has divided the memory into a few parts
(1), Stack stack: The compiler automatically assigns the release,
Store local Variables
(2) Heap area heap: The programmer is usually assigned to release, 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 is distributed in a way similar to a linked list.
Storage of malloc and new allocated space
(3), global (Static) static: The storage of global variables and static variables (global and local static variables) is placed in a block.
Initializes the global variables and static variables in an area,
Uninitialized global variables and static variables are placed in another adjacent area.
Released by the system after the program is finished.
(4), literal constant area: the constant string is placed here.
Released by the system after the program is finished.
(5), Program code area: Store the function body of the 2 code.
It was written by a predecessor, very detailed
Main.cpp
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 the constant area, p3 on the stack.
static int c = 0;//global (Static) initialization area
P1 = (char *) malloc (10);
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, the compiler may optimize it to a place with the "123456" that P3 points to.
}
Variable storage location