Why can#39;t I cause a seg fault?(为什么我不能导致段错误?)
问题描述
好的,无论出于何种原因,我都无法导致段错误.我想制作一个,以便我可以使用 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);
}
这篇关于为什么我不能导致段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我不能导致段错误?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
