我收到分段错误(核心转储)错误,但仅当我的一半代码运行时
I get a segmentation fault (core dumped) error but only when half my code runs
这段代码的目标是操作一个ASCII"图像",它以一个很长的字符串的形式出现,我在不同的行上打印出来。
.............
.............
.XXX.....X...
.XXX.....X...
.XXX.........
.XXX.........
.XXXXXXX.....
.XXXXXXX.....
.XXXXXXX.....
...........
看起来像这样
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <vector>
#include <string>
#include <cassert>
int main (int argc, char* argv[]) {
std::fstream img(argv[1]);
std::vector<char> path;
char x;
while (img >> x) {
path.push_back(x);
}
for (char i = 0; i < path.size(); ++i) {
if (i%13==0) {
std::cout << path[i] << std::endl;
}
else {
std::cout << path[i];
}
}
std::string replace = "replace";
std::string dilation1 = "dilation";
std::string erosion1 = "erosion";
std::string floodfill1 = "floodfill";
char old_char = argv[4][0];
char new_char = argv[5][0];
if (argv[3] == replace) {
for (char n=0; n<path.size(); ++n) {
if(path[n]==old_char) {
path[n]=new_char;
}
}
for (char i = 0; i < path.size(); ++i) {
if (i%13==0) {
std::cout << path[i] << std::endl;
}
else {
std::cout << path[i];
}
}
}
if (argv[3] == dilation1) {
std::cout << "this is ok" << std::endl;
}
}
这是我的代码。代码的替换部分旨在将其作为控制台的输入
./image_processing.out input4.txt output4_replace.txt replace X O
并将 X 替换为 O,它有效。但是,我转到膨胀函数,该函数将其作为控制台的输入。
./image_processing.out input4.txt output4_dilation.txt dilation X
不管它做什么。然而,我什至还没有走那么远,因为每当我尝试使用第二个"if"语句运行代码时,我都会得到这个
.............
.............
.XXX.....X...
.XXX.....X...
.XXX.........
.XXX.........
.XXXXXXX.....
.XXXXXXX.....
.XXXXXXX.....
分段故障(核心转储)
我不知道它为什么要这样做,也不知道为什么它只到最后一行。当我注释掉第二个 if 语句时,代码运行良好;但是,它只会替换。
像这样运行程序时:
./image_processing.out input4.txt output4_dilation.txt dilation X
只有 5 个参数,有效索引为 0 - 4。 该行:
char new_char = argv[5][0];
正在读取通过 argv 数组的末尾,因为这假设有 6 个参数。
相关文章:
- 我的字符计数代码计算错误.为什么
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 错误-我无法在VS2019中打开新的Qt项目
- 模板,函数使用错误的构造函数来复制我的对象
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 我的 int main() 中出现堆栈溢出错误
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- 无法在我的堆排序代码中找到错误.无法正确执行.C++
- C++出现控制台错误.我无法识别源代码的问题
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 我的模板类方法返回错误类型?
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- C++为什么我的编译器成功了,但我的计算机给出了调试错误?
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- 为什么我会得到细分错误:我的C 程序中的11个
- C++ BST 内存错误 - 我的删除有什么问题?