日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何解决 “ 段错误(吐核) ” ???

發布時間:2025/10/17 编程问答 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何解决 “ 段错误(吐核) ” ??? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、段錯誤的形式:? ? ? ?

在編程中以下幾類做法容易導致段錯誤,基本上是錯誤地使用指針引起的。

1)訪問系統數據區,尤其是往系統保護的內存地址寫數據最常見就是給一個指針以0地址。

2)內存越界(數組越界,變量類型不一致等): 訪問到不屬于你的內存區域。

解決方法:我們在用C/C++語言寫程序的時候,內存管理的絕大部分工作都是需要我們來做的。實際上,內存管理是一個比較繁瑣的工作,無論你多高明,經驗多豐富,難免會在此處犯些小錯誤,而通常這些錯誤又是那么的淺顯而易于消除。但是手工“除蟲”(debug),往往是效率低下且讓人厭煩的,本文將就"段錯誤"這個內存訪問越界的錯誤談談如何快速定位這些"段錯誤"的語句。

二、下面將就以下的一個存在段錯誤的程序介紹幾種調試方法:

test的代碼如下:

[root@localhost TEST]# cat test.c -n1 #include <stdio.h>2 int main(void)3 {4 printf("111");5 printf("222");6 int *ptr = NULL;7 *ptr = 1;8 }

1)使用命令 ? ?gcc -g -rdynamic test.c 然后gdb調試

[root@localhost TEST]# gcc -g -rdynamic test.c

?1.1)查找段錯誤:
這種方法也是被大眾所熟知并廣泛采用的方法,首先我們需要一個帶有調試信息的可執行程序,所以我們加上“-g -rdynamic"的參數進行編譯,然后用gdb調試運行這個新編譯的程序,具體步驟如下:

[root@localhost TEST]# gcc -g -rdynamic test.c [root@localhost TEST]# gdb ./a.out GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-80.el7 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-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /root/桌面/TEST/a.out...done. (gdb) r Starting program: /root/桌面/TEST/./a.out Program received signal SIGSEGV, Segmentation fault. 0x00000000004007d2 in main () at test.c:7 7 *ptr = 1; Missing separate debuginfos, use: debuginfo-install glibc-2.17-105.el7.x86_64 (gdb)

不用一步步調試我們就找到了出錯位置d.c文件的第4行,其實就是如此的簡單。

從這里我們還發現進程是由于收到了SIGSEGV信號而結束的。通過進一步的查閱文檔(man 7 signal),我們知道SIGSEGV默認handler的動作是打印”段錯誤"的出錯信息,并產生Core文件,由此我們又產生了方法二。

2)使用命令 ? ulimit 命令

?1.1)查找段錯誤:

[root@localhost TEST]# ulimit -c 0 [root@localhost TEST]# ulimit -c 1000 [root@localhost TEST]# ./a.out 段錯誤(吐核) [root@localhost TEST]# ls a.out core.15180 test test.c [root@localhost TEST]# gdb ./a.out core.15180 GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-80.el7 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-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /root/桌面/TEST/a.out...done. [New LWP 15180] Core was generated by `./a.out'. Program terminated with signal 11, Segmentation fault. #0 0x00000000004007d2 in main () at test.c:7 7 *ptr = 1; Missing separate debuginfos, use: debuginfo-install glibc-2.17-105.el7.x86_64

?

總結

以上是生活随笔為你收集整理的如何解决 “ 段错误(吐核) ” ???的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。