分段故障消息 [英] segmentation fault message

查看:78
本文介绍了分段故障消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在linux中编写了一个代码,删除了所有错误后,我遇到了分段错误消息,但我不明白它在说什么.如何清除此错误?
请提出一种快速有效的方法.

I wrote a code in linux and after removing all the errors, I meet the segmentation fault message, I don''t understand what it says. How can I remove this error?
please suggest a quick and effective way.
thanks.

推荐答案

一种快速有效的方法:
通过调试器运行代码.
A quick and effective way:
Run your code through a debugger.


Niklas和Harish都是正确的.前者将该工具命名为缩小错误的位置,后者为您提供了直觉查找的可能,一旦您找到了可能有问题的代码.

您收到的错误消息也可能有助于缩小错误的范围,但难度更大,而且通常没有帮助.当然,您还没有告诉我们它的意思.

在这种情况下,我通常要做的是
1.看错误
2.意识到它没有帮助(大部分时间)
3.考虑我执行的导致错误的步骤
4.查找一段我知道在错误发生之前不久已执行的代码
5.在那里设置断点
6.在调试模式下运行程序,执行相同的步骤,直到遇到断点
7.从那里开始逐步执​​行代码,直到我触发导致错误的指令或函数调用.
8.a)如果一个函数调用且该函数代码不平凡,请在此处设置一个断点,然后从6开始重复,这一次确保我进入该函数而不是在其上进行操作
8.b)否则,从6开始重复,但在错误发生之前停止,并检查所有变量的状态,并查看值是否是我期望的值
9.如果毕竟我仍然无法做到这一点,请问其他人.

所有这些都假定您知道如何在调试模式下构建程序,并且错误也确实在调试模式下发生.如果后者不正确,那么它将变得更加困难(恕我直言;但也更有趣)))
Both Niklas and Harish are correct. The former named the tool to narrow down where the error could be, the latter gives you a hunch what to look for once you''ve located a potentially problematic piece of code.

The error message you got may also be helpful in narrowing down the whereabouts of the error, but that is harder, and often not that helpful. And of course, you didn''t yet tell us what it said.

What I usually do in such a case, is
1. look at the error
2. realize it isn''t helping (most of the time)
3. Consider the steps I''ve performed that led to the error
4. Find a piece of code that I know has been executed shortly before the error occured
5. Set a breakpoint there
6. Run the program in debug mode, performing the same steps, until I hit the breakpoint
7. From there, step through the code until I trigger the instruction or function call that causes the error.
8.a) if a function call, and that functions code is not trivial, set a breakpoint here, and repeat from 6, this time making sure I step into that function rather than over it
8.b) else, repeat from 6, but stop right before the error occurs, and investigate the state of all variables, and see if the values are what I expect them to be
9. If after all this I still can''t make heads or tails of this, ask someone else.

This all assumes that you know how to build a program in debug mode, and the error does occur in debug mode as well. If the latter isn''t true, then it gets a lot harder (but more interesting too, IMHO ;) )


检查您是否尝试执行以下任一操作:
->取消引用NULL,
->取消引用未初始化的指针,
->取消引用已释放(或删除)的指针或超出范围的指针(对于在函数中声明的数组),
->注销数组的末尾.
->使用所有堆栈空间的递归函数.
Check if you are trying to do any of the following:
-> dereferencing NULL,
-> dereferencing an uninitialized pointer,
-> dereferencing a pointer that has been freed (or deleted, ) or that has gone out of scope (in the case of arrays declared in functions),
-> writing off the end of an array.
-> recursive function that uses all of the stack space.


这篇关于分段故障消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆