- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所有初始化的全局/静态变量将转到初始化数据部分。所有未初始化的全局/静态变量将转到未初始化的数据部分(BSS)。 BSS 中的变量在程序加载期间将获得值 0。
如果全局变量显式初始化为零(int myglobal = 0
),该变量将存储在哪里?
最佳答案
编译器可以自由地将此类变量放入bss
以及data
中。例如,GCC 有一个 special option控制此类行为:
-fno-zero-initialized-in-bss
If the target supports a BSS section, GCC by default puts variables that are initialized to zero into BSS. This can save space in the resulting code. This option turns off this behavior because some programs explicitly rely on variables going to the data section. E.g., so that the resulting executable can find the beginning of that section and/or make assumptions based on that.
The default is
-fzero-initialized-in-bss
.
尝试使用以下示例(test.c
文件):
int put_me_somewhere = 0;
int main(int argc, char* argv[]) { return 0; }
无选项编译(隐式 -fzero-initialized-in-bss
):
$ touch test.c && make test && objdump -x test | grep put_me_somewhere
cc test.c -o test
0000000000601028 g O .bss 0000000000000004 put_me_somewhere
使用 -fno-zero-initialized-in-bss
选项编译:
$ touch test.c && make test CFLAGS=-fno-zero-initialized-in-bss && objdump -x test | grep put_me_somewhere
cc -fno-zero-initialized-in-bss test.c -o test
0000000000601018 g O .data 0000000000000004 put_me_somewhere
关于c - 如果一个全局变量初始化为0,它会去BSS吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8721475/
我一直假设链接器分配了任何库的 bss 部分并将其映射到进程中。该部分的大小将取决于库报告的 bss 的大小。 我查看了进程的/proc/[PID]/maps 文件,并计算了加载库的 bss 部分的大
这个问题已经有答案了: No .BSS in PE file (2 个回答) 已关闭 6 年前。 我使用 NASM 组装了以下代码: global _start section .data v
我正在编写一些适用于 .o 文件的自定义 ELF 二进制后处理代码。部分处理包括对二进制数据执行重定位。输入文件由 GNU 汇编程序根据我自己的汇编代码生成。 看看 GNU 汇编程序生成的这些疯狂的重
“.bss”命令在 MSP430 汇编代码中起什么作用?例如,“.bss beep_cnt,2”对变量 beep_cnt2 有何作用? .bss beep_cnt,2
我知道 BSS 段存储未初始化的全局和静态变量并将它们初始化为零。但是如果全局/静态变量被初始化并且我的第二个问题是我读到 BSS 段不消耗内存,那么它存储这些变量的位置呢?谢谢 最佳答案 您可能读到
我一直在研究减少应用程序的内存占用。继上一个问题后:GDB - can I find large data elements in memory我已经找到并删除了大多数最大的罪魁祸首。 nm --si
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: .bss section in elf file 你好, BSS(Block started by symb
这个问题在这里已经有了答案: Why is the .bss segment required? (6 个答案) 关闭 4 年前。 .bss 部分的意义是什么,因为我们已经有单独的数据部分。它比数据
这个问题在这里已经有了答案: Why is the .bss segment required? (6 个答案) 关闭 6 年前。 在C编程中,当一个程序执行时,程序中使用的所有数据都会被放在不同的
这是简短的控制台应用程序示例 static char buffer[4096]; int main() { for(int i=0;i<4096;i++) { buffer[i
正如我们所知,.bss 包含未初始化的变量。如果在 C 代码中,程序员在使用它们之前初始化变量。那么在执行 C 代码之前.bss 不必为零。 我说得对吗? 谢谢 最佳答案 在 C 代码中,任何具有静态
我正在使用一个测试程序来了解内核版本为 2.6.32-279.el6.x86_64 的 linux 6.3 上的 C 内存模型。 首先我编译了下面的代码, #include int main(voi
我想在编译时声明我的 C 程序中的所有变量,例如: char cache[CACHE_SIZE]; char udp_ring[MAX_UDP_PACKET_SIZE*MAX_REQUESTS]; i
我知道 .bss 的默认对齐方式对于 GCC 是 8 字节,正如这个问题中提到的 Why the int type takes up 8 bytes in BSS section but 4 byte
我有一个功能: void testfunction() { static char_t theChar1 = 1; static unsigned char smallArray[1]; sta
据我所知,如果设置了 -fzero-initialized-in-bss,则未明确初始化的静态变量(并且对于 GCC,即使是那些明确初始化为零的变量,默认情况下就是这种情况)通常存储在 BSS 段中。
静态内存布局的 BSS 部分 [应该] 用于“未初始化的全局变量”或“设置为 0 的全局变量”。 我正在运行一些测试,突然注意到局部静态变量也在增加 BSS 段的大小。 例子:- 在任何静态变量之前
静态内存布局的 BSS 部分 [应该] 用于“未初始化的全局变量”或“设置为 0 的全局变量”。 我正在运行一些测试,突然注意到局部静态变量也在增加 BSS 段的大小。 例子:- 在任何静态变量之前
我正在学习虚拟内存管理和进程内存分配。并对此做一些实验。有以下一些容易混淆的地方: 案例1 #include int main() { return 0; } 编译代码并使用二进制文件运行
希望有人能帮助我了解我在大学学习的一个漏洞。 在c代码中有一个未绑定(bind)的strcat strcat(buffer, argv[1]); 目的是将该缓冲区溢出到保存的 EIP 中,并让它显示在
我是一名优秀的程序员,十分优秀!