The difference between heaps and stacks
The memory used by a program compiled by C + + is divided into the following sections
1. Stack (stack)-Automatically allocated by the compiler to release, store the function parameter value, local variable value and so on. Its
Operations are similar to stacks in data structures.
2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the end of the program may be the OS back
Inbox 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)-, 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 adjacent
An area. -released by the system after the program is finished.
4, literal constant area-the constant string is put here. Released by the system after the program is finished
5. Program code area-binary code that holds the function body.
Ii. Examples of procedures
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 zone
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.
}
The difference between a C + + heap and stack