代码本身非常简单。我正在使用Catch2进行单元测试,(我真的很喜欢它的界面)并闯入gdb
,但没有获得 Seg 的有用信息。所述简单代码引发的错误。
我确切地知道是什么导致了这个问题,但我不知道为什么,或者我会如何得到有问题的代码行gdb
(我已经广泛使用 Python 等价物,pdb
但 Python 中的错误似乎是更直接)。
翻牌.hpp
#ifndef FLOP
#define FLOP
class Flop {
private:
int tiles_[200][200][200];
public:
Flop();
}
#endif
翻牌.cpp
#include "Flop.hpp"
Flop::Flop() { }
test_Flop.cpp
#include "catch.hpp"
#include "Flop.hpp"
SCENARIO("I bang my head against a wall") {
Flop flop;
WHEN("I try to run this test") {
THEN("This program SEGFAULTs") {
REQUIRE(1==1);
}
}
}
main.cpp包含它应该包含的所有内容,以及下载的catch.hpp(按照教程的说明)。
我用: 编译g++ Flop.cpp test_Flop.cpp main.cpp -o run_test
它并用 运行它gdb -ex run --args ./run_test -b
,这允许 Catch2 进入调试器。结果是这样的:
Program received signal SIGSEGV, Segmentation fault.
0x0000555555566e9e in ____C_A_T_C_H____T_E_S_T____0() ()
使用回溯:
#0 0x0000555555566e9e in ____C_A_T_C_H____T_E_S_T____0() ()
#1 0x000055555557e15e in Catch::TestInvokerAsFunction::invoke() const ()
#2 0x000055555557d7b1 in Catch::TestCase::invoke() const ()
#3 0x0000555555577f0a in Catch::RunContext::invokeActiveTestCase() ()
#4 0x0000555555577c59 in Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) ()
#5 0x000055555557671b in Catch::RunContext::runTest(Catch::TestCase const&) ()
#6 0x00005555555797cc in Catch::(anonymous namespace)::TestGroup::execute() ()
#7 0x000055555557ab49 in Catch::Session::runInternal() ()
#8 0x000055555557a853 in Catch::Session::run() ()
#9 0x00005555555b6195 in int Catch::Session::run<char>(int, char const* const*) ()
#10 0x000055555558fdf0 in main ()
行。因此,SIGSEGV
表明我们尝试读取/写入进程无权访问的内存。如果在Flop.hpp中,我改为说int tiles_[10][10][10]
,那么一切正常。那么设置tiles_
为更大的大小是在某种程度上保留了一块无法访问的内存吗?我是 C++ 的新手(因此当我编写某些东西时,我实际上是在思考计算机中发生了什么)所以如果我错了请纠正我,但int tiles_[200][200][200]
不应该占用超过 32MB 的内存,对吧?
因此,我有几个问题:
- 为什么这会导致分段错误?
- 我怎样才能
gdb
让我进入有问题的代码行?这段代码的未简化版本总共有几百行。幸运的是,我的问题出现在班级定义的早期,但是将所有内容注释掉并(煞费苦心地)逐行取消注释仍然需要一段时间,这就是gdb
要防止的!