Stack, stack, stack
Stack and heap)
Concept: the heap and stack are both data structures and are a special storage area, mainly used to temporarily store data and addresses.
STACK: similar to the stack in the data structure. Stores parameters and local variables. This bucket is automatically allocated and released by the operating system without application.
Stack refers to a warehouse in the text, so you can put it in a similar warehouse before it can be taken out. It is similar to the last release of the first release. In fact, the stack address is a relatively high address. If the address growth direction is above, the stack address will grow downward.
Advantage: The reading speed is faster than the heap speed,
Disadvantages: low flexibility, limited data size and lifecycle. Data stored in stacks cannot be shared among multiple threads.
STACK: Stack in the same data structure is not a concept. A programmer must apply for and release the program on his own. If the program is not released, it may be withdrawn by the system at the end of the program. It follows the principle of first-in-first-out.
Advantage: it is flexible and can specify the size of memory allocated by itself, so the compiler cannot know its lifecycle.
Disadvantage: dynamic space can be allocated during running, which reduces the running speed.
Terms with the concept of storage include: Global de, text constant area, and program code area
Global zone: a variable with static modification. global variables and static variables are stored in one partition and released by the system after the program ends.
Text Constant Area: The system is released after a program ends. Some constant strings are stored in the zone.
Code area: the area where the code is converted to binary and stored.
Stack, for example, we only need results, so we don't have to worry about how to achieve the results.
Heap is responsible for its own results, and its own behavior will result in its own results.
Note:
1) All functions except the main function are stored in the global zone.
2) inside the main function, the static modifier is the global zone.
Variables are in the stack, and strings are in the constant zone.
Space allocation is caused by the statement in the heap.
This is written by a senior. It is very detailed.
// Main. cpp
Int a = 0; global initialization Zone
Char * p1; uninitialized globally
Main ()
{
Int B; stack
Char s [] = "abc"; stack
Char * p2; stack
Char * p3 = "123456"; 123456/0 is in the constant zone, and p3 is on the stack.
Static int c = 0; Global (static) initialization Zone
P1 = (char *) malloc (10 );
P2 = (char *) malloc (20 );
The allocated 10-byte and 20-byte areas are in the heap area.
Strcpy (p1, "123456"); 123456/0 is placed in the constant area, and the compiler may point it to "123456"
Optimized to one place.
}