从gdb角度分析,为什么C语言中的scanf函数的变量要用“&”表示首地址
2009-09-02 21:50
316 查看
gdb.c程序代码如下:
include<stdio.h>
int main(void)
{
int input=0;
printf("Enter an integer:");
scanf("%d",input);
printf("Twice the number you supplied is %d./n",2*input);
return 0;
}
用gdb默认方式编译
[quan@fedora c]$ gcc -Wall -o gdb gdb.c
gdb.c: In function ‘main’:
gdb.c:6: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
[quan@fedora c]$ ./gdb
Enter an integer:5
Segmentation fault
用ggdb3编译
[quan@fedora c]$ gcc -ggdb3 -Wall -o gdb gdb.c
gdb.c: In function ‘main’:
gdb.c:6: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
[quan@fedora c]$ ./gdb
Enter an integer:5
Segmentation fault
然后允许使用存储信息
[quan@fedora c]$ ulimit -c unlimited
[quan@fedora c]$ ./gdb
Enter an integer:5
Segmentation fault (core dumped)
调试加入core
[quan@fedora c]$ gdb gdb core
GNU gdb (GDB) Fedora (6.8.50.20090302-38.fc11)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
/home/quan/test/c/core: No such file or directory.
(gdb) r
Starting program: /home/quan/test/c/gdb
Enter an integer:5
Program received signal SIGSEGV, Segmentation fault.
0x005837b6 in _IO_vfscanf_internal () from /lib/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.10.1-4.i686
(gdb) bt
#0 0x005837b6 in _IO_vfscanf_internal () from /lib/libc.so.6
#1 0x00590239 in __isoc99_scanf () from /lib/libc.so.6
#2 0x08048447 in main () at gdb.c:6
(gdb) frame 2
#2 0x08048447 in main () at gdb.c:6
6 scanf("%d",input);
(gdb) print input
$1 = 0
(gdb) q
The program is running. Quit anyway (and kill it)? (y or n) y
原来input前没加&。
修改程序为:
include<stdio.h>
int main(void)
{
int input=0;
printf("Enter an integer:");
scanf("%d",&input);
printf("Twice the number you supplied is %d./n",2*input);
return 0;
}
然后编译运行
[quan@fedora c]$ gcc -ggdb3 -Wall -o gdb gdb.c
[quan@fedora c]$ ./gdb
Enter an integer:5
Twice the number you supplied is 10.
include<stdio.h>
int main(void)
{
int input=0;
printf("Enter an integer:");
scanf("%d",input);
printf("Twice the number you supplied is %d./n",2*input);
return 0;
}
用gdb默认方式编译
[quan@fedora c]$ gcc -Wall -o gdb gdb.c
gdb.c: In function ‘main’:
gdb.c:6: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
[quan@fedora c]$ ./gdb
Enter an integer:5
Segmentation fault
用ggdb3编译
[quan@fedora c]$ gcc -ggdb3 -Wall -o gdb gdb.c
gdb.c: In function ‘main’:
gdb.c:6: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
[quan@fedora c]$ ./gdb
Enter an integer:5
Segmentation fault
然后允许使用存储信息
[quan@fedora c]$ ulimit -c unlimited
[quan@fedora c]$ ./gdb
Enter an integer:5
Segmentation fault (core dumped)
调试加入core
[quan@fedora c]$ gdb gdb core
GNU gdb (GDB) Fedora (6.8.50.20090302-38.fc11)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i586-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
/home/quan/test/c/core: No such file or directory.
(gdb) r
Starting program: /home/quan/test/c/gdb
Enter an integer:5
Program received signal SIGSEGV, Segmentation fault.
0x005837b6 in _IO_vfscanf_internal () from /lib/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.10.1-4.i686
(gdb) bt
#0 0x005837b6 in _IO_vfscanf_internal () from /lib/libc.so.6
#1 0x00590239 in __isoc99_scanf () from /lib/libc.so.6
#2 0x08048447 in main () at gdb.c:6
(gdb) frame 2
#2 0x08048447 in main () at gdb.c:6
6 scanf("%d",input);
(gdb) print input
$1 = 0
(gdb) q
The program is running. Quit anyway (and kill it)? (y or n) y
原来input前没加&。
修改程序为:
include<stdio.h>
int main(void)
{
int input=0;
printf("Enter an integer:");
scanf("%d",&input);
printf("Twice the number you supplied is %d./n",2*input);
return 0;
}
然后编译运行
[quan@fedora c]$ gcc -ggdb3 -Wall -o gdb gdb.c
[quan@fedora c]$ ./gdb
Enter an integer:5
Twice the number you supplied is 10.
相关文章推荐
- 改变指针指向的地址为什么需要二维指针,从汇编角度分析
- linux 反汇编分析变量地址并用gdb修改运行中的程序内存变量实验
- 从源码角度分析,为什么会发生Fragment重叠?
- 如果改变了形参数组的值 从而在调用函数期间 形参数组就和实参数组共占同一段内存单元 为什么形参数组从实参数组那里得到起始地址后 请问各位C语言高手:当用数组名作函数参数时 也就该变了实参数组的值
- C语言中返回局部变量或临时变量的地址的错误
- 「C语言」「概念」常量和变量的表示及应用
- 以杨辉三角为例,从内存角度简单分析C语言中的动态二维数组
- C语言为什么不能精确表示浮点数
- GCC编译器局部变量地址分配为什么总是从低地址向高地址增加?
- 数组名可以用变量表示吗之分析程序
- Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)
- 内存角度理解C语言中变量的存储形式
- c语言 输出变量的地址,动态的观察内存的分配。
- 从编译器角度分析C语言中数组名和指针的区别
- c语言不用临时变量交换两个数程序分析
- 第02天C语言(11):变量-内存分析
- Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)
- DEBUG模式下, 内存中的变量地址分析
- 为什么C语言中使用的地址是假的
- C语言学习 - 变量为什么要进行初始化