Stack
In the computer field, the stack is a concept that can not be ignored, but many people even the computer professional people do not have a clear stack is actually two data structures.
Points:
Heap: Random in order
Stack: Advanced back out
The difference between heap and stack
First, the preparation of knowledge-Program memory allocation
The memory used by a program compiled by C + + is divided into the following sections
1, stack area (stack)-by the compiler automatically assigned to release, store the function of the parameter values, local variables and other values. The operation is similar to the stack in the data structure.
2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the program at the end may be reclaimed by the OS. Note that it is different from the heap in the data structure, the distribution is similar to the list, hehe.
3, Global area (static)--------------------------------------------------------------- -System release after the program is finished
4, literal constant area-the constant string is here. Released by system after program is finished
5, program code area-the binary code that holds the function body.
Second, the example procedure
This is written by a predecessor, very detailed
//main.cpp
int a = 0; 全局初始化区
char *p1; 全局未初始化区
main()
{
int b; 栈
char s[] = "abc"; 栈
char *p2; 栈
char *p3 = "123456"; 123456\0在常量区,p3在栈上。
static int c =0; 全局(静态)初始化区
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, and the compiler may optimize it with the "123456" that the P3 points to as a place.
}