Heap, stack, Bss, Data, text, rodata View instruction + explanation [hand notes]

Handy notes, bss, data, text, rodata, heap, stack

Insert picture description here

    The Bss segment, also called the zero segment for the convenience of memory, is usually used to store 未初始化or store 初始化为0global 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

  1. #define a 10
  2. char * = “hello world”;
  3. 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 newspace applications. free deleteFree up space.

  1. char* src= (char* )malloc(100);
  2. 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

Guess you like

Origin blog.csdn.net/weixin_44972997/article/details/114824060
BSS