初始化后未初始化的全局变量去了哪里?

发布于 2024-08-01 17:20:14 字数 232 浏览 5 评论 0原文

我在学习的时候遇到了一个小问题。 我知道C中未初始化的全局变量被分配给可执行ELF文件中的.bss部分。 但是当我开始使用它们时,它们会发生什么? 即它们在堆上还是其他地方占有一席之地?

我试图通过打印(仍未初始化)全局变量的地址来找出答案,该变量

printf("%x",&glbl);

总是返回相同的值 0x80495bc...为什么?

I struck a little problem when learning. I know that uninitialized global variables in C are assigned to the .bss section in the executable ELF file. But what happens to them when I start to use them?
I.e. do they get a place on the heap or somewhere else?

I tried to find out by printing the address of the (still uninitialized) global variable with

printf("%x",&glbl);

which always return the same value 0x80495bc... Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溇涏 2024-08-08 17:20:14

当操作系统加载您的程序时,它会从程序的地址空间中分配足够的存储空间来存储 .bss 部分中的所有内容,并将所有内存清零。 当您分配、读取或获取变量的地址时,您正在操作分配来为 .bss 部分提供存储的内存。

When the OS loads your program, it allocates enough storage from your program's address space to store everything in the .bss section and zeros all of that memory. When you assign or read from or take the address of the variable, you're manipulating that memory that was allocated to provide storage for the .bss section.

柏拉图鍀咏恒 2024-08-08 17:20:14

全局变量总是获得静态内存,如果它们未初始化,则它们在二进制文件中没有空间,但当二进制文件加载到进程内存空间时,它们确实会在内存中获得它。

The global variables always get static memory, if they're uninitialized they don't have space in the binary, but they do get it in memory when the binary is loaded to the process memory space.

清风夜微凉 2024-08-08 17:20:14

BSS 是以可执行(或 ELF)格式定义的占位符。 因此它不占用磁盘空间,而只是指定链接器或加载器应分配哪些内存区域。

确切的操作取决于操作系统。 既然你提到了ELF,我假设它是用于嵌入式系统的。 如果您构建 ROMmable 代码,则链接器 cmd 文件会将 BSS 映射到静态地址区域。

如果您为操作系统(即Linux)进行构建,操作系统的加载程序将执行重定位过程,其中它将所有以可执行格式标记为相对的位置映射到内存中的物理或逻辑位置。

因为您提到总是看到相同的值,这表明该过程对于您的系统是可重复的。 当您更改链接器文件(即地址区域)、链接顺序(即模块将以不同的顺序分配空间)或操作系统时,预计会看到变化。

无论您是否使用 BSS 值,您运行的进程的地址都将保持不变。

The BSS is a placeholder defined in your executable (or ELF) format. So it does not take up disk space, but only specifies what memory region should be allocated by the linker or loader.

The exact operation depends on the operating system. Since you refer to ELF, I assume it is for use in an embedded system. If you build for ROMmable code, your linker cmd file will map the BSS to a static address region.

In case you build for an operating system (i.e. Linux), the loader from the operating system will perform a relocation pass, in which it maps all locations marked as relative in the excecutable format to physical or logical locations in memory.

Because you mention always seeing the same value, this indicates that the process is repeatable for your system. Expect to see changes when you change linker files (i.e. address regions), link order (i.e. modules will get assigned space in a different order) or operating system.

Wether or not you use the BSS values, the address will remain the same for the process you run.

沙与沫 2024-08-08 17:20:14

该 BSS 部分在进程地址空间中被赋予一个内存块,就像代码和堆栈部分(以及任何其他 ELF 可能具有的)一样。 一旦到达那里,他们就不会去任何地方。 加载器安排事情然后调用进程入口点。

That BSS section is given a memory block in the process address space just like the code and stack sections (and any other ELF may have). Once there, they don't go anywhere. The loader arranges things then calls the process entry point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文