为什么不能引起seg故障? [英] Why can't I cause a seg fault?

查看:205
本文介绍了为什么不能引起seg故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论出于什么原因,我遇到问题,导致seg故障。我想生成一个,所以我可以使用 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 ++ 作为我的编译器。任何人都可以给我一些代码,保证segfault?

I'm using Ubutnu 9.10 and g++ as my compiler. Can anyone show me some code that is guaranteed to segfault?

推荐答案

它不可能尝试和可靠做它解引用指针。 >
这是因为应用程序处理内存的方式在编译器和编译器之间也是不同的,不同的编译器也有不同的选择(调试/释放模式不同)。

It impossable 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 handeled differently).

你可以做的是使用信号显式地提高segfault:

What you can do is explicitly raise the segfault using a signal:

#include <signal.h>

int main()
{
    raise(SIGSEGV);
}

这篇关于为什么不能引起seg故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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