码迷,mamicode.com
首页 > 系统相关 > 详细

linux Ubuntu(Segmentation fault)段错误出现原因及调试方法

时间:2014-12-07 23:00:58      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   strong   

  

   在linux下编译了一个程序,尝试运行的时候出现:

  Segmentation fault (core dumped)

  

  初步确认为...完全不知道是什么玩意。

  于是找度娘了。

----------------------------------------------------------------------------

  

  

  出现原因

  原来这个东西叫做段错误,就程序运行的时候出现内存错误。有很多原因会导致这样的内存错误,但是应该把这些问题归结于程序的错误,那么程序是出现了什么样的错误了呢,为什么编译的时候明明都通过了。

  

 

  调试方法

  gdb调试:

  

  1.使能产生core文件

  查看core文件大小设置

  ulimit -c

  如果为0则无法产生core文件

  ulimit -c 1000

  2.再次运行新编译的程序

  ./mian

  ls

  core文件出现了

  3.调试

  

root@ubuntu:/home/linux-ljy# gdb mian core GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04 Copyright (C) 2012 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 "i686-linux-gnu". For bug reporting instructions, please see: <http://bugs.launchpad.net/gdb-linaro/>... Reading symbols from /home/linux-ljy/mian...(no debugging symbols found)...done. [New LWP 2939]

warning: Can‘t read pathname for load map: Input/output error. [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1". Core was generated by `./mian‘. Program terminated with signal 11, Segmentation fault. #0  0x08048f04 in main ()

  查看错误信息,黄色背景部分才是真正有用的

  这么说来错误发生在main()函数里面了。

 

  再仔细研究一下我写的程序,本来应该是加载一副图片并显示出来的,但是由于找不到图片,即没有输入,导致空指针被加载到了一段内存中。

 

  网上有一篇写得很好的文章,具体步骤参考其中http://wenku.baidu.com/link?url=vWrcQVtSL97AlqPzkTGVdUoVJaPR_WOiu9Ok_5dibE72lygVMTe9iI2aNLy5gSs8YbQOpEzdfo-x_YilMyola2uuzEi6rs4lFBnJ3oe3D7O

 

linux Ubuntu(Segmentation fault)段错误出现原因及调试方法

标签:style   http   io   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/shazi909/p/4150056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!