为什么我不能导致段错误? [英] Why can't I cause a seg fault?

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

问题描述

好的,无论出于何种原因,我都无法导致段错误.我想制作一个,以便我可以使用 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);
}

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

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