Segmentation fault 定位

看了酷壳上这个贴

http://coolshell.cn/articles/11377.html

#include <stdio.h>
struct str{
    int len;
    char s[0];
};
 
struct foo {
    struct str *a;
};
 
int main(int argc, char** argv) {
    struct foo f={0};
    if (f.a->s) {
        printf( f.a->s);
    }
    return 0;
}


试了试怎么看出错在哪一行。
编译后直接运行会提示 segmentation fault.重新编译 gcc -o app structshuzu.c -g,然后执行 ./app,找到core 文件。注意有些系统上可能看不到core文件,我用的就是这样的。需要用ulimit -c 1000设置一个大小吧。然后gdb app core.30506,然后执行 where就可以看到是哪一行有问题了


[****@*****]$ gdb app core.30506
GNU gdb (GDB) 7.6
Copyright (C) 2013 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 "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /data2/home/********/chap03/app...done.
[New LWP 30506]


warning: Can't read pathname for load map: Input/output error.
Core was generated by `./app'.
Program terminated with signal 11, Segmentation fault.
#0  0x00007fd0cd3eacfa in strchrnul () from /lib64/libc.so.6
(gdb) where
#0  0x00007fd0cd3eacfa in strchrnul () from /lib64/libc.so.6
#1  0x00007fd0cd3a432f in vfprintf () from /lib64/libc.so.6
#2  0x00007fd0cd3af44a in printf () from /lib64/libc.so.6
#3  0x00000000004004fd in main (argc=1, argv=0x7fff8ccec188)
    at structshuzu.c:14

看了下面两篇博客
http://blog.csdn.net/dongqinliuzi/article/details/39892941
http://blog.csdn.net/xinanzhung/article/details/27338365

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值