为什么我不能导致段错误?
好的,无论出于何种原因,我都无法导致段错误.我想制作一个,以便我可以使用 gdb
来查看如何调试一个.我已经尝试了 维基百科 文章中的两个示例,但都不起作用.
OK for whatever reason I'm having trouble causing a seg fault. I want to produce one so that I can use gdb
to see how to debug one. I have tried both examples from the Wikipedia article yet neither work.
第一个:
char *s = "Hello World!";
*s = 'H';
第二个例子:
int main(void)
{
main();
}
我使用 Ubutnu 9.10 和 g++
作为我的编译器.谁能给我看一些保证段错误的代码?
I'm using Ubutnu 9.10 and g++
as my compiler. Can anyone show me some code that is guaranteed to segfault?
推荐答案
解引用指针不可能尝试可靠.
这是因为应用程序处理内存的方式可能因编译器而异,而且在具有不同选项的同一编译器中(调试/发布模式处理方式不同).
It impossible to try and reliable do it dereferencing pointers.
This is because how the application handles memory can vary from compiler to compiler also across the same compiler with different options (debug/release mode handled differently).
您可以做的是使用信号显式引发段错误:
What you can do is explicitly raise the segfault using a signal:
#include <signal.h>
int main()
{
raise(SIGSEGV);
}
相关文章