您的位置:首页 > 编程语言 > C语言/C++

【C语言】【unix c】同名局部变量之间关系

2017-08-17 23:04 453 查看
代码:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int j;
void count(void) {
static int i = 1;
printf("++i1 = %d\n", ++i);
printf("i1 address:%p\n", &i);
return;
}
void count1(void) {
static int i;
printf("++i2 = %d\n", ++i);
printf("i2 address:%p\n", &i);
return;
}

int main(void) {
int i;
for(i = 0; i < 5; i++) {
count();
count1();
}
printf("j address is:%p\n", &j);
getchar();
return 0;
}

命令: tarena@ubuntu:~/day/day26$ ./a.out
结果: pid:4237
++i1 = 2
i1 address:0x804a01c
++i2 = 1
i2 address:0x804a028
++i1 = 3
i1 address:0x804a01c
++i2 = 2
i2 address:0x804a028
++i1 = 4
i1 address:0x804a01c
++i2 = 3
i2 address:0x804a028
++i1 = 5
i1 address:0x804a01c
++i2 = 4
i2 address:0x804a028
++i1 = 6
i1 address:0x804a01c
++i2 = 5
i2 address:0x804a028
j address is:0x804a02c

测试编译连接后的状态信息:
命令: tarena@ubuntu:~/day/day26$ nm a.out
结果: 08049f28 d _DYNAMIC
08049ff4 d _GLOBAL_OFFSET_TABLE_
0804868c R _IO_stdin_used
w _Jv_RegisterClasses
08049f18 d __CTOR_END__
08049f14 d __CTOR_LIST__
08049f20 D __DTOR_END__
08049f1c d __DTOR_LIST__
08048800 r __FRAME_END__
08049f24 d __JCR_END__
08049f24 d __JCR_LIST__
0804a020 A __bss_start
0804a014 D __data_start
08048640 t __do_global_ctors_aux
080483c0 t __do_global_dtors_aux
0804a018 D __dso_handle
w __gmon_start__
08048632 T __i686.get_pc_thunk.bx
08049f14 d __init_array_end
08049f14 d __init_array_start
08048630 T __libc_csu_fini
080485c0 T __libc_csu_init
U __libc_start_main@@GLIBC_2.0
0804a020 A _edata
0804a030 A _end
0804866c T _fini
08048688 R _fp_hw
080482f8 T _init
08048390 T _start
0804a020 b completed.7108
08048444 T count
08048485 T count1
0804a014 W data_start
0804a024 b dtor_idx.7110
08048420 t frame_dummy
U getchar@@GLIBC_2.0
U getpid@@GLIBC_2.0
0804a01c d i.2461 //在程序中两个函数的变量名是一样的,但是为了区分,在编译后局部变量的后面会有一个编号
0804a028 b i.2470 // 变量名前面的d表示已初始化的数据段,b是未初始化的局部变量,也在数据段
0804a02c B j //变量前面大写字母表示全局变量,小写的代表局部变量
080484c6 T main
U printf@@GLIBC_2.0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unix c语言 局部变量