Handy notes, bss, data, text, rodata, heap, stack
The Bss segment, also called the zero segment for the convenience of memory, is usually used to store
未初始化
or store初始化为0
global variables and static variables. The uninitialized value will be its初始化为0
.
It is worth noting that the variable of type bss does not occupy the actual disk space. It only occupies the memory space during operation.
The Data segment, also called the rw segment, indicates that the data in this area can be read or written.
存放的初始化的全局变量和静态(static)变量
, Which occupies the file size space and also occupies the memory space at runtime, so不易大量使用
the reason.
The Rodata segment, referred to as the ro segment for short, means that the data in this area can only be read but not written. What is stored is constant data (that is
不能修改的数据
).
For example, the following three situations
- #define a 10
- char * = “hello world”;
- const a =0
The. text section stores the program
二进制代码
, that is, the machine instructions executed by the CPU, and is read-only.
The head heap area, the heap is used to store the memory segment dynamically allocated during the running of the process. Its size is not fixed and can be dynamically expanded or reduced. The use of
malloc new
space applications.free delete
Free up space.
- char* src= (char* )malloc(100);
- studeng* zhangsan =new student();
stack Stack area, storage
局部变量值 函数的参数
, that is, in the {} scope. (Does not include the block static const), which is automatically allocated and released by the compiler.
for example
#include <stdio.h>
define aa 1
int a=1;
int b;
main()
{
int c;
char *d=“123455”;
static int e=1;
char* f =(char* )malloc(100);
}
The ro segment has: aa d
data segment has: ae
bss segment has: b
stack area has: c
heap area has: f
Common exam questions
1
char* a;
char *d=“123455”;
strcpy(a,d);
Error, d is read-only and cannot be copied.
2
const char *arr ="123";
char* brr ="123";
const char crr[]="123";
char drr[]="123";
Subject: The difference between the above four lines of code.
The first one is consistent with the second one. It was originally stored in the ro segment. const originally modified the value pointed to by arr and cannot be modified by arr, but "123" is originally in the ro segment and cannot be changed. So the effect is the same.
The third is in the ro segment, and the fourth is on the stack, but the compiler may make some optimizations and put it on the ro segment.
View instructions
Compile link
gcc test.c -o test
Pay attention to the size of the file
ls -l test
View the size of each section (stack and head section)
size test
The viewing segment table stores various information of each segment, such as the name of the segment, the type of the segment, the offset of the segment in the elf file, and the size of the segment.
readelf -S test
Similar function
objdump -h