Components of the program
As shown above. Program from code to stack address from small to large,
There is an unreadable area before. text;
The size of. Text +. Rodata +. Data +. BSS +. Heap +. Stack is determined at the program compilation stage;
-
. text segment: the area where the program code is stored;
-
. rodata segment: ro means read only, rodata means read-only constant data segment;
- Constants may not be stored in. rodata, but may also be stored in. text, such as some immediate numbers;
- String constants are stored in the. text section and can be determined during compilation;
- The const modified variable is stored in the. text section and determined during the running of the program
-
. data section: stores initialized global variables; Initialized global variables occupy storage space in both file and operation;
-
. bss segment: store uninitialized global variables or initialize global variables to 0;
- Do not occupy storage space in the file;
- Occupy storage space during operation;
-
Heap section: heap area, which is used to dynamically apply for memory. The programmer applies for memory and releases memory;
- malloc: used to allocate a specified size of memory;
- realloc: used to adjust / reallocate an existing memory;
- free: used to release memory that is no longer used;
- Memory leak: memory allocation is not released;
- Program crash: released nonexistent memory;
- Buffer Overflow: write or read out of range memory;
- You can use the valgrind program memory;
-
. stack section: stack area, which stores local variables and function parameters, and the system applies for memory and releases memory;
- Stack is a data structure. The data in the stack is accessed in the following ways: first in first out, last in and last out;
- Generally, the stack address increases downward (low address). Without pushing an address, the top of the stack expands to the low address. For each Pop element, the top of the stack retreats to the high address;
-
Stack is followed by inaccessible system code area;
Example description. data and. bss segments
The variables in the. data section occupy both file space and running space
// File name: data.c int data_arr[1024 * 1024] = {1, }; // . data segment, initialized to 1, with a space size of 4M; int main(int argc, char *argv[]) { return 0 } $ gcc -g data.c -o data $ ls -l data -rwxrwxr-x 1 zach zach 4203984 9 June 8:27 data objdump -h data | grep \\.data 24 .data 00400020 0000000000601020 0000000000601020 00001020 2**5
It can be seen from the above that there are more than 4M file space and running space;
Variables in the. bss section do not occupy file space, but occupy running space;
// File name: bss.c int data_arr[1024 * 1024] = {0, }; // . bss segment, initialized to 0, with a space size of 4M; int main(int argc, char *argv[]) { return 0; } $ gcc -g exercise.c -o bss $ ls -l bss -rwxrwxr-x 1 zach zach 9664 9 June 8:31 bss $ objdump -h bss | grep \\.bss 25 .bss 00400020 0000000000601040 0000000000601040 00001030 2**5
It can be seen from the above that the. bss section does not occupy file space and occupies running space;
Code description
int data_arr2[1024 * 1024] = {1, }; // . data section int data_arr1[1024 * 1024] = {0, }; // . bss segment char * p1; // . bss segment int main() { int b; // Stack.stack char s[] = "abs"; // Stack.stack char *p2; // Stack.stack char *p3 = "12345"; // "12354 \ 0" is in the read-only constant area. rodata, and p3 is in the stack area static int c = 0; / / Global static area,.bss paragraph p1 = (char *)malloc(10);// Allocate 10 bytes in heap p2 = (char *)malloc(20);// Allocate 20 bytes in heap strcpy(p1,"123456"); // "123456 \ 0" is in the constant area. The constant area should be between code and Data }
Memory allocation
There are three main distribution methods:
- Allocate in static storage:
- The variables involved include: string constants, static applied variables, and global variables;
- The memory segments involved are:. rodata segment,. data segment,. bss segment;
- The variable life cycle of static storage area is the running cycle of the whole program;
- Allocate on stack:
- Local variables;
- Formal parameters of function;
- When the function exits, it will be cleared automatically;
- Dynamic memory allocation on heap:
- molloc requests memory;
- realloc redistribution;
- Free free memory;