Program allocation in memory

storage area in c

1. Divided into these storage areas in c

1. Stack-automatically allocated and released by the compiler

2. Heap-Generally allocated and released by the programmer, if the programmer does not release, it may be reclaimed by the OS when the program ends

3. Global area (static area), the storage of global variables and static variables are placed together, initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent block area. -Release at the end of the program

4. There is also a place for constants. -Release at the end of the program

The variables defined in the function body are usually on the stack, and the functions allocated by malloc, calloc, realloc, etc. are allocated on the heap.

The global variables defined outside of all functions are stored in the global area (static area) wherever the static modifier is added. The static variables defined outside of all functions are valid in this file and cannot be externed to other files. Use, static defined in the function body means that it is only valid in the function body.

In addition, strings such as "adgfdf" in the function are stored in the constant area.

//main.c
int a = 0;      // 全局初始化区
char *p1;      // 全局未初始化区
void main()
{
    
    
    int b;            // 栈区
    char s[] = "abc"; // 栈区
    char *p2; // 栈区
    char *p3 = "123456"; // p3在栈区;   "123456\0" 在常量区, 
 
    static int c =0;      // 全局(静态)初始化区
    p1 = (char *)malloc(10);
    p2 = (char *)malloc(20); // 分配得来的10和20字节的区域就在堆区 
    strcpy(p1, "123456");    // "123456\0" 放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方。
} 

Storage area in c++

2. In C++, the memory is divided into 5 areas, they are heap, stack, free storage area, global/static storage area and constant storage area

1. The stack is the storage area for variables that are allocated by the compiler when needed and are automatically cleared when not needed. The variables inside are usually local variables, function parameters, etc.

2. Heap is the memory block allocated by new. Their release compiler does not care, but is controlled by our application. Generally, a new corresponds to a delete. If the programmer does not release it, the operating system will automatically reclaim it after the program ends.

3. Free storage area is those memory blocks allocated by malloc etc. It is very similar to heap, but it uses free to end its life.

4. Global/static storage area, global variables and static variables are allocated to the same block of memory. In the previous C language, global variables are divided into initialized and uninitialized. There is no such distinction in C++. They share the same Occupy the same memory area.

5. Constant storage area. This is a special storage area. Constants are stored in them and cannot be modified (of course, you can also modify it through improper means)

Theoretical knowledge of heap and stack

Third, heap and stack theoretical knowledge of
the mode of application

stack: automatically allocated by the system. For example, declare a local variable int b in the function; the system automatically opens up space for b in the stack

Heap: The programmer needs to apply by himself and specify the size. The malloc function in c

Such as p1 = (char *)malloc(10);

Use the new operator in C++

Such as p2 = (char *)malloc(10);

But note that p1 and p2 are in the stack.

Guess you like

Origin blog.csdn.net/DR5200/article/details/112668215