当前位置:网站首页>Memory distribution in C language and program running (BSS segment, data segment, code segment, stack)
Memory distribution in C language and program running (BSS segment, data segment, code segment, stack)
2022-07-20 19:22:00 【Micro embedded electron】
BSS paragraph :(bss segment) It is usually used to store uninitialized Of Global variables A memory area of .BSS It's English Block Started by Symbol For short .BSS Segment belongs to static memory allocation .
for example :
#include <stdio.h>
int g1=0, g2=0, g3=0;
int max(int i)
{
int m1=0,m2,m3=0,*p_max;
static n1_max=0,n2_max,n3_max=0;
p_max = (int*)malloc(10);
printf(" Print max Program address \n");
printf("in max: 0x%08x\n\n",max);
printf(" Print max Incoming parameter address \n");
printf("in max: 0x%08x\n\n",&i);
printf(" Print max Address of static variable in function \n");
printf("0x%08x\n",&n1_max); // Print the memory address of each local variable
printf("0x%08x\n",&n2_max);
printf("0x%08x\n\n",&n3_max);
printf(" Print max Address of local variable in function \n");
printf("0x%08x\n",&m1); // Print the memory address of each local variable
printf("0x%08x\n",&m2);
printf("0x%08x\n\n",&m3);
printf(" Print max Function malloc Allocation address \n");
printf("0x%08x\n\n",p_max); // Print the memory address of each local variable
if(i) return 1;
else return 0;
}
int main(int argc, char **argv)
{
static int s1=0, s2, s3=0;
int v1=0, v2, v3=0;
int *p;
p = (int*)malloc(10);
printf(" Print all global variables ( Initialized ) Memory address of \n");
printf("0x%08x\n",&g1); // Print the memory address of each global variable
printf("0x%08x\n",&g2);
printf("0x%08x\n\n",&g3);
printf("======================\n");
printf(" Print program initial program main Address \n");
printf("main: 0x%08x\n\n", main);
printf(" Print the main parameter address \n");
printf("argv: 0x%08x\n\n",argv);
printf(" Print the memory address of each static variable \n");
printf("0x%08x\n",&s1); // Print the memory address of each static variable
printf("0x%08x\n",&s2);
printf("0x%08x\n\n",&s3);
printf(" Print the memory address of each local variable \n");
printf("0x%08x\n",&v1); // Print the memory address of each local variable
printf("0x%08x\n",&v2);
printf("0x%08x\n\n",&v3);
printf(" Print malloc Assigned heap address \n");
printf("malloc: 0x%08x\n\n",p);
printf("======================\n");
max(v1);
printf("======================\n");
printf(" Print the starting address of the subfunction \n");
printf("max: 0x%08x\n\n",max);
return 0;
}
Print the results :
You can roughly check the allocation of the whole program in memory :
It can be seen that , Incoming parameter , local variable , They are all distributed at the top of the stack , With the increase of subfunctions, it grows downward .
Call address of the function ( Function run code ), Global variables , Static variables exist in the lower part of the allocated memory , and malloc The allocated heap exists on these memories , And grow up .
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the operating system , A process is a program in execution ( Of course, it includes system resources ), A living specimen of the program code actually being executed . So how to divide the logical address space of the process ?
quote :
chart 1 I made a simple explanation (Linux Under the system )
On the left is UNIX/LINUX The execution file of the system , On the right is the division of the logical address space of the corresponding process .
The first is the stack area (stack), The stack is automatically allocated and released by the compiler , Stores the parameter values of the function , The value of a local variable, etc . It operates like a stack in a data structure . Stack applications are automatically allocated by the system , For example, apply for a local variable inside the function int h, At the same time, judge whether the applied space is less than the remaining space of the stack , If less than , Make room for it in the stack , Provide memory for programs , Otherwise, an exception will be reported indicating that the stack overflows .
The second is heap (heap), The heap is usually released by the programmer , If programmers don't release , At the end of the program, the OS Recycling . Notice that it's not the same as the heap in the data structure , The distribution method is similar to the linked list . The application of heap is operated by the programmer himself , stay C Use in malloc function , and C++ Use in new Operator , But the application process of heap is more complicated : When the system receives an application for a program , Will traverse the free memory address record list , In order to find the first heap node whose space is larger than the applied space , Then delete the node from the free node list , And allocate the space of the node to the program , It should be noted here that in some cases , The first address of the newly applied memory block records the size of the memory block allocated this time , In this way delete In especial delete[] The memory space can be released correctly .
Next is the global data area ( Static zone ) (static), Global and static variables are stored in one block , Initialized global and static variables in one area , Uninitialized global variables and uninitialized static variables are in another adjacent area . In addition, the text constant area , Constant strings are put here , There is a system release after the program .
Finally, the program code area , Put the binary code of the function body .
For example :
int a = 0; // Global initialization area
char *p1; // Global uninitialized area
int main()
{
int b; // Stack
char s[] = "abc"; // Stack
char *p2; // Stack
char *p3 = "123456"; //123456\0 In the constant area , and p3 On the stack .
static int c =0; // overall situation ( static state ) Initialization area
p1 = (char *)malloc(10);
p2 = (char *)malloc(20); // Well distributed 10 and 20 The byte area is in the heap area .
strcpy(p1, "123456"); //123456\0 Put it in the constant area , The compiler may match it with p3 The point is "123456" Optimize into a place .
return 0;
}
边栏推荐
- Ceph在手天下我有!
- 限制su命令与sudo机制提升 nmap和控制台命令netstat
- 过过 适配器模式
- WPF RadioButton 样式 (1)
- 当护网遇上毕业季,同学们准备好了吗,我们要做什么准备
- S3c2440 u-boot migration - norflash driver support - s29al016u-boot version: 2008.10 development board: mini2440
- WPF DataGrid realizes the effect of selecting cells
- MCU debugging - the use of event recorder
- [problem solving] the port number is occupied
- Brief introduction of temperature measurement module of mlx90640 infrared thermal imager
猜你喜欢
随机推荐
Leetcode 115.不同的子序列
MLX90640 红外热成像仪测温模块简要介绍说明
shell跑数命令
WPF DataGrid 实现 选中单元格 效果
芯片低功耗睡眠模式下与看门狗的使用
STM32F103 learning notes (VI) independent watchdog and window watchdog
The industry's first "best practice map of live video technology" was released!
"Error in enabling windows function netfx3" (error cause, detailed analysis and solution) and installation on windows server2012 occur during the installation of SQL Server 2012 NET Framework 3
基于STM32的CAN通讯测试:让地球仪转起来
CRC8 CRC16 查表法
【C语言】文件的处理与操作
过过 适配器模式
开发者必读:2022年移动应用运营增长洞察白皮书
MCU debugging - the use of event recorder
STM32F103学习笔记(六) 独立看门狗and窗口看门狗
ES6语法扩展与新特性
Avalonia - NETCORE image processing
Arm assembly - BIC, Orr
“蔚来杯“2022牛客暑期多校训练营1 补题题解(未完成)
WPF MVVM mouse double click event