代码::blocks, SIGSEGV,分段故障.在吗?().
Code::blocks, SIGSEGV, Segmentation fault. In? ()
我必须使用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
您正在解引用一个未初始化的指针。这是未定义的行为,你的程序是没有意义的。
编译器可以自由地生成任何它喜欢的东西(包括导致段错误或根本不做任何事情的代码)-基本上,所有的赌注都是取消的,你可以相信什么都没有,你甚至不能指望你的调试器向你展示任何正常的东西,因为它只需要与编译器生成的东西一起工作-这可能是无论。
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么不;名字在地图上是按顺序排列的吗
- C++:将控制台输出存储在宏中更好吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在某些循环内使用vector.push_back时出现分段错误
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 在决定是通过参考还是通过价值时,尺寸真的是一个问题吗
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 类型总是使用其大小存储在内存中吗
- 我可以在 C++ 中的函数体之外进行操作吗?
- 在C++中声明一个函数时,它需要有函数本身的参数吗
- 在使用分段寻址模式的 16 位系统上,"size_t"、"uintptr_t"、"intptr_t"和"ptrdiff_t"类型的实际大小是多少?
- 在 std::sort() 中的自定义函数中,>(大于)运算符与 <(小于运算符)的交换会导致分段错误吗?
- 代码在我的系统上运行良好,但在USACO上出现分段错误.有什么问题吗?
- 代码::blocks, SIGSEGV,分段故障.在吗?().
- 在类分段错误中存储Lambda