1. C/C ++ compiled program, memory is divided into the following parts (from low address to high address ):
Code (text): In the lowest available memory address area, stores the binary code of program functions and constants used in the program. function calls are implemented through the function address.
Initialized data: including initialized variables and initialized static variables, which are automatically released after the program ends.
Uninitialized data: including uninitialized and uninitialized static variables. The compiler will automatically release the uninitialized and uninitialized variables when the compiler initializes them to 0.
STACK: the bottom of the stack is located in the high address area of the memory, increasing from the high address to the low address. the compiler is responsible for applying for and releasing functions, including function parameters and local variables. generally, the worker capacity of a process is limited, and the lifecycle of the worker is limited, which is the reason for automatic release.
For example, if a class student exists, create a local object student st;
This object will automatically call the Destructor at the end of the lifecycle to release the memory occupied by this object.
Heap: the heap is located in the low address area of the memory, increasing from the low address to the high address. the stack is manually applied and released by programmers. If the applied memory is not released, memory leakage will occur. at the same time, the advantage is that we can determine the time when the heap variable (object) exists, and the heap operations are visible to the programmer, as long as we ensure release. note: In general, it is very dangerous to release the applied memory, that is, do not release the memory in function B applied in function.
Command line parameters and environment variables: these are in the highest available memory address zone, including command line parameters and environment variables, and are automatically released.
2. Sample Code:
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 area (text), and P3 is in 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 (text), and the compiler may optimize it into a place with the "123456" pointed to by P3.
}
3. Advantages and Disadvantages of heap and heap:
There is no size limit on heap allocation, and the size limit on 32-bit systems is 4 GB;
The shard size is limited, generally only a few MB.
Shard allocation efficiency is high and does not cause memory fragmentation;
However, the heap allocation efficiency is not high, and a large number of new/delete operations will cause a large number of memory fragments.
The memory is released by the compiler;
The releasing of memory in the heap is manually handled by the programmer. If the heap is not released, memory leakage will occur.
The program built on the memory pool management can optimize this point. It stores the deleted memory in the table. When new is required, it can be viewed in the table first. If there is a suitable space, it can be used, if there is no suitable new operation. the memory pool is a complex technology, including its application sequence, size matching, retrieval methods, and recovery mechanisms. I will write relevant articles if I have the chance.
4. Summary:
This is a popular phrase in many BBS,
The difference between stack and stack can be seen in the following metaphor:
Using Stacks is like eating at a restaurant, just ordering food (sending an application), paying for it, and eating (using it). If you are full, you can leave, without having to worry about the preparation work, such as cutting and washing dishes, as well as the cleaning and tail work such as washing dishes and flushing pots, his advantage is fast, but his degree of freedom is small.
Using heap is like making your favorite dishes. It is troublesome, but it suits your taste and has a high degree of freedom.