Detailed explanation of memory space distribution in C language

The C language memory space is divided into the following 5 blocks:

BSS segment: (bss segment) usually refers to a memory area used to store uninitialized global variables in the program . BSS is the abbreviation of English Block Started by Symbol. The BSS segment is a static memory allocation.

Data segment : The data segment usually refers to a memory area used to store initialized global variables in the program. The data segment is a static memory allocation.
Code segment:  Code segment (code segment/text segment) usually refers to a memory area used to store program execution code . The code in this area is read-only . In code snippets, string constants also belong to this section.
Heap : The heap is used to store memory segments that are dynamically allocated during the running of the process. Its size is not fixed and can be dynamically expanded or reduced.
Stack (stack) : The stack is also called a stack. In addition to storing the local variables of the program, the stack is used to pass parameters and return values ​​when a function is called. Due to the first-in-last-out feature of the stack, the stack is particularly convenient for saving/restoring the call scene.
Let's take a look at these 5 parts through a screenshot in the book "Advanced Programming in UNIX Environment":

The left side is the internal organizational structure of the executable file under the UNIX/LINUX system, and the right side is the division of the executable file into the process logical address space (ie, memory). 


The first is the stack area (stack). The stack is automatically allocated and released by the compiler to store function parameter values, local variable values, etc. It operates like a stack in a data structure. The application for the stack is automatically allocated by the system. For example, apply for a local variable int h inside the function. At the same time, it is judged whether the applied space is less than the remaining space of the stack. If it is less than the remaining space of the stack, create space for it in the stack to provide memory for the program. Otherwise, An exception will be reported indicating stack overflow.    

Next is the heap. The heap is generally allocated and released by the programmer. If the programmer does not release it, it may be reclaimed by the OS when the program ends. Note that it is different from the heap in the data structure, and the allocation method is similar to a linked list. Heap application is operated by the programmer himself. The malloc function is used in C, and the new operator is used in C++. However, the heap application process is more complicated: when the system receives the application from the program, it will traverse and record the free memory address. linked list to find the first heap node with a space larger than the requested space, then delete the node from the free node linked list, and allocate the node's space to the program. It should be noted here that there are some In this case, the first address of the newly applied memory block records the memory block size allocated this time, so that the memory space can be released correctly when delete, especially delete[].

Next is the global data area (static area) (static). Global variables and static variables are stored together. Initialized global variables and static variables are in one area, and uninitialized global variables and uninitialized static variables are in the same area. Another adjacent area. In addition, in the text constant area, constant strings are placed here and are released by the system after the program ends. Finally, there is the program code area, which contains the binary code of the function body.

Let’s give an example:

int a = 0;                 // 全局初始化区
char *p1;                  // 全局未初始化区 


int main()
{
        int b;                     // 栈
        char s[] = "abc";          // 栈
        char *p2;                  // 栈
        char *p3 = "123456";       // 123456 在常量区,而 p3 在栈上

        static int c =0;          // 全局(静态)初始化区 

        p1 = (char *)malloc(10);

        p2 = (char *)malloc(20);  // 分配的10和20字节的区域就在堆区

        strcpy(p1, "123456");     // 123456 放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方。

        return 0;
}

Guess you like

Origin blog.csdn.net/gzg1500521074/article/details/50444845