Run program memory analysis. (C language)

  The program is stored in the hard disk. When the program is to be executed, the operating system loads the code in the hard disk into the memory, finds the main function as the program entry, and starts execution. The process of program execution is completed in memory. Let's talk about memory management in C program execution:

It can be divided into four major partitions (also can be divided into five partitions): heap area (heap). Managed by the operating system, malloc/free/delete and other codes are established, and are generally allocated and released by the programmer (ie, apply and release). If the programmer does not release, it may be reclaimed by the operating system after the program ends.

stack area. It is automatically allocated and released by the compiler to store local variables, function parameter values, etc.

Global area: store constants and global variables, static variables, managed by the operating system. (This area can actually be divided into two memory storage areas, the constant area and the global area.)

Program area: the code area, which stores binary code.

ps: Only the C language is discussed now. C++ adds the concept of free storage area in order to distinguish between new and malloc.

During the execution of the function, the operating system only allocates a four-area model of memory for it, namely a stack, a heap, and a stack. . . . The function call is actually the process of pushing and popping the stack.



Practice drawing a memory analysis diagram based on the code:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325956278&siteId=291194637