![](/img/trans.png)
[英]Getting 'Segmentation fault' error when trying to load text file into a dynamically allocated 2d array
[英]Segmentation fault when trying to print out/load a 2d array
当我尝试运行我的代码时,它可以毫无问题地进行编译。 但是,当我尝试运行该程序时,出现了line x: segmentation fault
,x是错误所在的行,但是每次我尝试再次运行该程序时,它都会改变+1,这似乎很奇怪。 下面是相关代码:
#include <iostream>
#include <fstream>
#include <string>
#include "image.h" // Has the prototypes for the functions
using namespace std;
int** load(string imageFile, int &length, int &height) {
ifstream file(imageFile);
if(file.is_open()) {
file >> length;
int** array = new int*[length];
file >> height;
for(int i = 0; i < length; i++) {
array[i] = new int[height];
for(int j = 0; j < height; j++) {
file >> array[i][j];
if(array[i][j] > 255 || array[i][j] < 0) {
cout << "Image is corrupted." << endl;
file.close();
return 0;
}
}
}
file.close();
return array;
}
else {
cout << "Unable to open file." << endl;
return 0;
}
}
void show(int **image, int length, int height) {
cout << "The height of the matrix is: " << height << endl;
cout << "The length of the matrix is: " << length << endl;
cout << "The matrix is: " << endl;
for(int i = 0; i < length; i++) {
for(int j = 0; j < height; j++) {
cout << " " << image[i][j];
}
cout << endl;
}
}
int main() {
int height = 0;
int length = 0;
int **image = load("../resource/imagecorrupted.txt", length, height);
image = load("../resource/image.txt", length, height);
show(image, length, height);
}
这是输出: Image is corrupted. Image is corrupted. //Not sure why this shows twice to be honest The height of the matrix is: 8 // but that seems like the least of my worries The length of the matrix is: 10 The matrix is: -bash: line xx: xxxxx Segmentation fault
Image is corrupted. Image is corrupted. //Not sure why this shows twice to be honest The height of the matrix is: 8 // but that seems like the least of my worries The length of the matrix is: 10 The matrix is: -bash: line xx: xxxxx Segmentation fault
不知道是什么原因造成的,我们将不胜感激!
编辑:
我完全忘记显示输入的含义。 我道歉。 它们是: 10 8 0 255 255 255 0 0 255 255 255 0 255 0 255 255 0 0 255 255 0 255 255 255 0 255 255 255 255 0 255 255 255 255 255 0 255 255 0 255 255 255 255 255 255 355 0 0 255 255 255 255 255 255 255 255 0 0 255 255 255 255 255 255 255 0 255 255 0 255 255 255 0 0 0 255 255 255 255 0 0 0
那就是image.txt
包含的image.txt
。 imagecorrupted.txt
是相同的,其中一个值从255切换为355(这是有意的失败)。 10
和8
是矩阵的长度/高度。
编辑2:
尝试在每个load
调用之间添加delete
功能,但没有用,尽管我确定这里有些东西我没有得到。 这是使用的代码:
void free(int **image, int &length, int &height) {
if(image) {
for(int i = 0; i < length; i++) {
if(image[i]) {
delete[] image[i];
}
}
delete[] image;
}
}
我主要所做的是:
int **image = load("../resource/imagecorrupted.txt", length, height);
free(image, length, height);
image = load("../resource/image.txt", length, height);
首先,您有内存泄漏。 为了避免泄漏并有更好的边界检查,您应该考虑使用std::vector<std::vector<int>>
而不是int**
。
您的当机是由于第二次失败。 当第二次load
失败时,它将返回0,即nullptr
(在这种情况下,建议使用nullptr
而不是0)。 稍后, show
尝试取消引用此nullptr
-导致分段错误。
如果您坚持使用原始指针,而不是向量指针,或者使用次优的unique_ptr指针,那么您必须确保在load
失败以及连续两次成功load
(以及结束时)清理分配。
由于整数355,第二个调用已损坏。此外,您的列和行似乎已转置(行被视为列,列被视为行)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.