看了酷壳上这个贴
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