赞
踩
先直接贴上一段wikipedia中的描述:
BSS in C
In C, statically-allocated objects without an explicit initializer are initialized to zero (for arithmetic types) or a null pointer (for pointer types). Implementations of C typically represent zero values and null pointer values using a bit pattern consisting solely of zero-valued bits (though this is not required by the C standard). Hence, the BSS segment typically includes all uninitialized objects (both variables and constants) declared at file scope (i.e., outside any function) as well as uninitialized static local variables (local variables declared with the keyword); static local constants must be initialized at declaration, however, as they do not have a separate declaration, and thus are typically not in the BSS section, though they may be implicitly or explicitly initialized to zero. An implementation may also assign statically-allocated variables and constants initialized with a value consisting solely of zero-valued bits to the BSS section.
Peter van der Linden, a C programmer and author, says, "Some people like to remember it as 'Better Save Space.' Since the BSS segment only holds variables that don't have any value yet, it doesn't actually need to store the image of these variables. The size that BSS will require at runtime is recorded in the object file, but BSS (unlike the data segment) doesn't take up any actual space in the object file."
Data
The data area contains global and static variables used by the program that are explicitly initialized with a non-zero (or non-NULL) value. This segment can be further classified into a read-only area and read-write area. For instance, the string defined by char s[] = "hello world" in C, or a C statement like int debug = 1 outside the main function, would be stored in initialized read-write area. Also, a C statement like const char* string = "hello world", placed outside the main function, stores the string literal "hello world" in the read-only area (however, some C implementations may store it in the code segment instead), and the character pointer variable string in initialized read-write area.
BSS
The BSS segment, also known as uninitialized data, is usually adjacent to the data segment. The BSS segment contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code. For instance, a variable defined as static int i; would be contained in the BSS segment.
简单说来,Data段中存放的是显式初始化为非0值的全局或静态变量,并会占据目标文件空间,而Bss段中存放的是未初始化或初始化为0值的全局或静态变量,且不会实际占据目标文件的空间。
下面就通过几个实验来验证一下上述说法。
1)非静态局部变量
#include
#include
#include
int main(int argc, char *argv[]){
int array[1024*1024] = {'a'};
int i;
printf("Hello World!\n");
for(i=0; i<1024*1024; i++){
array[i] += i;
}
printf("%d %d %d\n", array[0], array[1024], array[1024*1024-1]);
printf("See You World!\n");
return 0;
}
编译该程序并查看结果:
gcc -g -o bss bss.c
$ ls -l ./bss
-rwxrwxr-x 1 xxx xxx 9895 Sep 9 20:10 ./bss
$ size ./bss
text data bss dec hex filename
1491 520 16 2027 7eb ./bss
可见,程序大小为9895字节,data段和bss段的大小分别为520和16;
2)未初始化的静态局部变量
#include
#include
#include
int main(int argc, char *argv[]){
static int array[1024*1024];
int i;
printf("Hello World!\n");
for(i=0; i<1024*1024; i++){
array[i] += i;
}
printf("%d %d %d\n", array[0], array[1024], array[1024*1024-1]);
printf("See You World!\n");
return 0;
}
查看结果:
$ ls -l ./bss
-rwxrwxr-x 1 xxx xxx 9870 Sep 9 20:26 ./bss
$ size ./bss
text data bss dec hex filename
1386 512 4194336 4196234 40078a ./bss
可见,目标文件大小为9870,与之前相比没有太大变化,通过size命令可以看到实际bss段的大小增长了很多;
3)初始化为非0值的静态局部变量
#include
#include
#include
int main(int argc, char *argv[]){
static int array[1024*1024] = {'a'};
int i;
printf("Hello World!\n");
for(i=0; i<1024*1024; i++){
array[i] += i;
}
printf("%d %d %d\n", array[0], array[1024], array[1024*1024-1]);
printf("See You World!\n");
return 0;
}
查看结果:
$ ls -l ./bss
-rwxrwxr-x 1 xxx xxx 4204198 Sep 9 20:31 ./bss
$ size ./bss
text data bss dec hex filename
1386 4194832 16 4196234 40078a ./bss
可以看到,这时目标文件的大小增长了很多,同时通过size命令看到占用的是Data段;
4)未初始化的全局变量
#include
#include
#include
int array[1024*1024];
int main(int argc, char *argv[]){
int i;
printf("Hello World!\n");
for(i=0; i<1024*1024; i++){
array[i] += i;
}
printf("%d %d %d\n", array[0], array[1024], array[1024*1024-1]);
printf("See You World!\n");
return 0;
}
查看结果:
$ ls -l ./bss
-rwxrwxr-x 1 xxx xxx 9873 Sep 9 20:34 ./bss
$ size ./bss
text data bss dec hex filename
1386 512 4194336 4196234 40078a ./bss
可以看到,这时占用的是Bss段,且不会占据目标文件的空间;
5)初始化为非0的全局变量
#include
#include
#include
int array[1024*1024] = {'b'};
int main(int argc, char *argv[]){
int i;
printf("Hello World!\n");
for(i=0; i<1024*1024; i++){
array[i] += i;
}
printf("%d %d %d\n", array[0], array[1024], array[1024*1024-1]);
printf("See You World!\n");
return 0;
}
查看结果:
$ ls -l ./bss
-rwxrwxr-x 1 xxx xxx 4204201 Sep 9 20:38 ./bss
$ size ./bss
text data bss dec hex filename
1386 4194832 16 4196234 40078a ./bss
此时占用的是Data段,且会占用目标文件的空间;
好久没有写文章了,这篇略水,但也说明“实践出真知”,能够帮助我们更好的理解知识点;
希望对大家有帮助:)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。