主要有以下几种变量#
1.全局变量
1.1 未初始过的全局变量
1.2 初始过的全局变量,其值为0
1.3 初始过的全局变量,其值不为0
1.4 未初始过的静态全局变量
1.5 初始过的静态全局变量,值不为0
1.6 初始过的静态全局变量,值不为0
2.局部变量
2.1 未初始过的局部变量
2.2 初始过的局部变量,其值为0
2.3 初始过的局部变量,其值不为0
2.4 未初始过的静态局部变量
2.5 初始过的静态局部变量,其值为0
2.6 初始过的静态局部变量,其值不为0
//exam1.c
#include <stdio.h>
int var_global_init_0 = 0;
int var_global_init = 2;
int var_global_un_init;
static int var_static_global_un_init;
static int var_static_global_init = 5;
static int var_static_global_init_0 = 0;
int func()
{
static int var_static_local_un_init;
static int var_static_local_init = 5;
static int var_static_local_init = 0;
return 0;
}
int main()
{
func();
return 0;
}
对编译过后的文件使用命令objdump -t命令全不信息
book@book-desktop:~/exam/inline_asm$ objdump -t exam1_elf
exam1_elf: file format elf32-little
SYMBOL TABLE:
00008074 l d .text 00000000 .text
000100a8 l d .data 00000000 .data
000100b8 l d .bss 00000000 .bss
00000000 l d .comment 00000000 .comment
00000000 l d *ABS* 00000000 .shstrtab
00000000 l d *ABS* 00000000 .symtab
00000000 l d *ABS* 00000000 .strtab
00000000 l df *ABS* 00000000 exam1.c
000100b8 l O .bss 00000000 $d
000100a8 l O .data 00000000 $d
000100ac l O .data 00000004 var_static_global_init
000100bc l O .bss 00000004 var_static_global_init_0
000100c0 l O .bss 00000004 var_local_un_init.0
000100b0 l O .data 00000004 var_local_init.1
000100c4 l O .bss 00000004 var_local_init_0.2
000100c8 l O .bss 00000004 var_static_local_un_init.3
000100b4 l O .data 00000004 var_static_local_init.4
000100cc l O .bss 00000004 var_static_local_init_0.5
00008074 l F .text 00000000 $a
000100d0 l O .bss 00000004 var_static_global_un_init
000100a8 g O .data 00000004 var_global_init
000100a8 g .data 00000000 __data_start
000100b8 g *ABS* 00000000 __bss_start__
00008074 g F .text 00000018 func
000100d8 g *ABS* 00000000 __end__
000100d8 g *ABS* 00000000 __bss_end__
000100b8 g *ABS* 00000000 __bss_start
000100d8 g *ABS* 00000000 _bss_end__
000100d4 g O .bss 00000004 var_global_un_init
000100d8 g *ABS* 00000000 _end
000100b8 g *ABS* 00000000 _edata
000100b8 g O .bss 00000004 var_global_init_0
0000808c g F .text 0000001c main
book@book-desktop:~/exam/inline_asm$
.bss段中包含哪些内容
var_static_global_init_0
var_static_global_un_init
var_global_un_init
var_global_init_0
var_local_un_init.0
var_local_init_0.2
var_static_local_un_init.3
var_static_local_init_0.5
book@book-desktop:~/exam/inline_asm$ objdump -t exam1_elf | grep "\.bss"
000100b8 l d .bss 00000000 .bss
000100b8 l O .bss 00000000 $d
000100bc l O .bss 00000004 var_static_global_init_0
000100c0 l O .bss 00000004 var_local_un_init.0
000100c4 l O .bss 00000004 var_local_init_0.2
000100c8 l O .bss 00000004 var_static_local_un_init.3
000100cc l O .bss 00000004 var_static_local_init_0.5
000100d0 l O .bss 00000004 var_static_global_un_init
000100d4 g O .bss 00000004 var_global_un_init
000100b8 g O .bss 00000004 var_global_init_0
.data段中包含哪些内容
var_static_global_init
var_local_init.1
var_static_local_init.4
var_global_init
book@book-desktop:~/exam/inline_asm$ objdump -t exam1_elf | grep "\.data"
000100a8 l d .data 00000000 .data
000100a8 l O .data 00000000 $d
000100ac l O .data 00000004 var_static_global_init
000100b0 l O .data 00000004 var_local_init.1
000100b4 l O .data 00000004 var_static_local_init.4
000100a8 g O .data 00000004 var_global_init
000100a8 g .data 00000000 __data_start
结论:经过实验发现初始化过的变量(初始值不为0)不管是局部,还是静态还是全局都会存放在data段中
未初始化和初始化为0的变量都存在bss段里面
网友评论