如何解释GDB回溯以确定分段错误?
How to interpret a GDB backtrace in order to determine a segmentation fault?
我正在上入门级编程课。我知道分段错误是由于内存存储中的错误造成的。我编写的程序应该获取一个提供给我们的文件,该文件是代码,其中包含解码它的说明,然后打印解码的消息。
我们有几个测试用例,我的代码为其中一些运行,但不适用于最后一个。我今天第一次了解了用于调试的 GDB,并使用回溯完整来尝试查找错误,但我不完全确定如何解释它给我的内容。
这是我编写的代码。 **编辑的代码输出
当我回溯时,这就是它告诉我的。
#2 0x0000000000401523 in main () at main.cpp:42
second = 61 '='
third = 72 'H'
msg = 0x606308
i = 8
chars = ""
first = 90 'Z'
numMess = 8
out = <incomplete type>
name = "input4.txt"
in = <incomplete type>
arr = "IJKLMNOPQRSTUVWXYZABCDEFGH"
我不知道回溯告诉我什么,我不确定如何处理这些信息来发现和修复我的错误。
来自跟踪的提示是
i = 8
chars = ""
numMess = 8
i
等于numMess
,chars
为空。
为什么这很重要?查看numMess
的来源,我们看到它用于调整msg
指向的动态数组的大小,msg
稍后按i
进行索引。当i
等于numMess
时,msg[i]
是越界的。
那么这是怎么发生的呢?
string chars;
getline(in, chars); // this doesn't make much sense. Reconsider it
for (chars; std::getline(in, chars); i < numMess) {
这就是出错的地方。for 循环应该看起来像
for (initializer ; exit condition ; iterator to advance the loop)
但
for (chars; // does nothing. Compiler may be warning you
std::getline(in, chars); // loop exits on failed read
i < numMess) { // does nothing to advance the loop
此处没有任何内容可以阻止i
超过numMess
i < numMess
因为它没有被用作退出条件。对,但是为什么std::getline(in, chars);
在到达文件末尾时不踢出循环?文件末尾的空行。 已成功将chars
设置为空字符串。
for ( ; // nothing happening here
i < numMess and std::getline(in, chars) ;
++i) { // and remove the ++i from the end of the loop.
让你摆脱目前的困境,并可能给你一个做你想做的事的程序。还没有测试过。
但是,如果文件中有错误并且文件在到达numMess
之前退出怎么办?这就是保罗·麦肯齐(PaulMckenzie)正在做的事情。你最好根本不相信numMess
,而选择更像的东西
int numMess = 0;
in >> numMess; // not that we care
vector<Messages> msg;
//read file
int i = 0;
string chars;
while (std::getline(in, chars)) { // get each line. Note this gets the empty line, too
Messages temp; // make a temporary for storage
temp.messageNum = i + 1;
temp.codedMessage = chars;
//decode file
for (char &j : chars) {
if (j - 'A' >= 0 && j - 'A' < 26)
j = arr[j - 'A'];
}
temp.decodedMessage = chars;
msg.push_back(temp);
}
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 我是如何在这段代码中出现分段错误的
- 创建结构的数组时遇到分段错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 在 c++ 中实现 Trie 时出现分段错误
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么我遇到分段错误?
- 动态类的分段错误(家庭作业问题)
- 分段错误 - 读取初始化指针的数组
- 如何摆脱C ++中的分段错误错误?
- 使用 CTYPE 时出现分段错误
- 为什么代码给出分段错误?