C language code memory space layout: heap, stack, data segment, bss segment, text segment (code segment)

1. Program memory allocation

======================
Reference:
https://blog.csdn.net/weixin_44564958/article/details/122873470

======================

From low address to high address, the memory is divided into: text segment (text), data segment (.data), bss segment, heap (heap), stack (stack)

Insert image description here

  • Text segment (text): (also called code segment)
    mainly saves the binary program code of the process, and also saves readable constants and variables, such asString constant
  • The data segment (.data)
    stores global variables and static variables that have been initialized not to 0.
  • bss section
    uninitialized global variables (global variables default to 0 if not initialized) or global variables initialized to 0;
    Note: The bss segment represents a placeholder, no space is allocated for the data in this segment, only the required space size is recorded;
  • The heap
    is used to dynamically allocate memory. It is allocated using functions such as malloc and released by functions such as free and delete. If the memory is not released manually, it will be released by the operating system after the program ends;
    Note: It is a data structure that extends from low memory addresses to high addresses;
  • The stack
    stores dynamic local variables (note that global variables and static variables are stored in the static area), and function parameters and return values ​​are also stored here;
    Note: It is a data structure in which the memory address extends from high address to low address;

=======
-Global /static storage area:
As shown in the figure, the global/static storage area contains two parts: data segment (data) and bss segment;

-Constant storage area: a
constant that can determine the storage size during compilation. This constant is not allowed to be modified;

- Note:
- Stored in RAM: data segment (.data), bss segment, heap, stack (stack);
- Stored in ROM: text segment (text), constant storage;

2. Example

The following piece of code shows which areas of memory the variables and constants are placed in:

//main.c源文件

int a = 0; //全局初始化为0   全局/静态存储区中bss段
char *p1; //全局未初始化区   全局/静态存储区宗data段 

int main()
{
    
    
	int b; //局部变量     栈
	char s[] = "abc"; //局部变量   栈
	char *p2; //局部变量  栈
	char *p3 = "123456"; //p3在栈上;123456\0 这个值是常量,放在常量存储区(代码段text);
	static int c = 0;  //静态变量初始化为0  全局/静态储存区中bss段 
	p1 = (char *)malloc(10);//malloc分配保存在  堆
	p2 = (char *)malloc(20);//malloc分配保存在  堆
	strcp(p1, "123456");    //123456\0 这个值放在常量存储区(代码段中)
	
	return 0;
}

3. Regarding the issue of whether global variables and static variables should be initialized to 0 in the bss section or in the data section.

Regarding the storage of the data segment and bss segment in the global/static storage area:
Many blogs say that
the data segment places initialized global variables and static variables, and
the bss segment places uninitialized global variables and static variables.
However, in the notes of teacher Zhu Youpeng Above is:
data segmentplaced alreadyInitialization is not 0Global variables and static variables (added initialization is not 0)
bss sectionplaceNot initialized or initialized to 0Global variables and static variables (not initialized or initialized to 0 are placed in the bss section)

As follows: int a; //Global variables that are not initialized (placed in the bss section)
Insert image description here
int a = 0; //Global variables initialized to 0
Insert image description here
int a = 1; //Global variables that are not initialized to 0 (placed in the data section) )
Insert image description here
Summary:
1. From the actual size obtained above, global variables and static variables are placed in the bss segment when initialized to 0;

Guess you like

Origin blog.csdn.net/weixin_42640280/article/details/127214528