代码::blocks, SIGSEGV,分段故障.在吗?().

Code::blocks, SIGSEGV, Segmentation fault. In? ()

本文关键字:在吗 分段 blocks SIGSEGV 代码 故障      更新时间:2023-10-16

我必须使用Code::Blocks调试我的c++程序中的段错误。不幸的是,堆栈跟踪没有正确显示,而是看到?? ()

下面是一个简单的例子:

#include <iostream>
using namespace std;
int main()
{
    int *plop;
    cout << *plop << endl;
    return 0;
}

调试器说:

程序收到信号SIGSEGV,分段错误。在? ?() ()

但是我期待一些更有用的东西,比如"In main()"


编辑:这里是构建日志,如果它有帮助的话
-------------- Build: Debug in tests (compiler: GNU GCC Compiler)---------------
g++.exe -Wall -fexceptions -g -O -pedantic -Wextra -std=c++0x -std=c++14  -c D:Ctestsmain.cpp -o objDebugmain.o
D:Ctestsmain.cpp: In function 'int main()':
D:Ctestsmain.cpp:8:14: warning: 'plop' is used uninitialized in this function [-Wuninitialized]
     cout << *plop << endl;
              ^
g++.exe  -o bintests.exe objDebugmain.o  -s  
Output file is bintests.exe with size 542.00 KB

第二次编辑:终于解决了:)

对于那些通过谷歌来到这里的人:在我的情况下检查了条带符号-s和优化器-O编译器选项,这些选项与-g冲突,因为它们在编译的代码中删除了调试符号。

感谢大家的回答

必须像下面这样初始化int *plop;指针,然后打印值:

#include <iostream>
using namespace std;
int main()
{
    int *plop = new int(15);
    // *plop = 120; // you can change the plop value as custom
    cout << *plop << endl;
    return 0;
}

结果将为:15

您正在解引用一个未初始化的指针。这是未定义的行为,你的程序是没有意义的。

编译器可以自由地生成任何它喜欢的东西(包括导致段错误或根本不做任何事情的代码)-基本上,所有的赌注都是取消的,你可以相信什么都没有,你甚至不能指望你的调试器向你展示任何正常的东西,因为它只需要与编译器生成的东西一起工作-这可能是无论

相关文章: