我所知道的是全局和静态变量存储在.data
段中,未初始化的数据存在于.bss
段中.我不明白的是为什么我们有未初始化变量的专用段?如果未初始化的变量在运行时分配了值,那么该变量是否仅存在于.bss
段中?
在以下程序中, a
是在.data
段中,并且b
在.bss
段中; 那是对的吗?如果我的理解是错误的,请纠正我.
#include <stdio.h>
#include <stdlib.h>
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
int b[20]; /* Uninitialized, so in the .bss and will not occupy space for 20 * sizeof (int) */
int main ()
{
;
}
Run Code Online (Sandbox Code Playgroud)
另外,请考虑以下程序,
#include <stdio.h>
#include <stdlib.h>
int var[10]; /* Uninitialized so in .bss */
int main ()
{
var[0] = 20 /* …
Run Code Online (Sandbox Code Playgroud)