A brief analysis of memory layout in C language
- 2020-04-02 01:27:28
- OfStack
This section focuses on a few concepts:.text.data.bss The heap The stack Static storage Read-only storage, etc
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201308/20130615111527328.png ">
From program to a.out to.text .data The BSS It is the process of compiling principle
Mapping the program from a.out to the corresponding memory address space is done by the operating system, which is done when the operating system creates the process, in the structure that describes the process.
We often say that the heap is used when applying for dynamic memory, malloc.
The stack is used to switch between functions, that is, to store local variables in a function. The heap and stack are allocated by the operating system, so they are not in a.out.
Static storage is used to hold global variables, static variables, understand the static use, that is, including.bss (uninitialized) and.data (initialized).
Read only storage is used to hold some constants, strings, read only data, understand that char * p="hello!!" It's not the wild pointer.
The program segment (.text) is used to hold executable code.
To sum up: the read-only storage area, also known as the code area, contains the read-only constant char* p="hello!!" # define PI (3.14) Enumerated type Program code.
So memory is generally divided into four areas: the heap The stack Static area Read only storage
Note: more than just data is stored on the stack The program's machine code should also be saved And then I'm going to convert to.text
Note: some of them are not very clear, such as the generation of the segments in a.out, the mapping of a.out to memory, how the heap and stack are generated after the mapping of a.out, and according to what? These questions are all about compiling principles and operating systems.