从 CSV 文件填充双指针矩阵
Fill a double pointer matrix from CSV file
我想用 CSV 中的值填充双指针二维数组。我不想在填充之前读取 csv 文件来获取数组的大小,我想用指针而不是 std::vector 来完成它。我当前的代码是这样的
std::pair<int, int> readFile(const std::string &filename, int **matrix) {
std::fstream file{filename, std::ios::in};
if (file.is_open()) {
std::string line{};
int col{0};
int row{0};
while (std::getline(file, line)) {
// Check how many cols there are
int len = std::count(begin(line), end(line), ',') + 1;
// Allocate a vector with size the cols found before
int *tmp = reinterpret_cast<int *>(calloc(len, sizeof(int)));
col = 0;
// Fill the temp vector with the read numbers
while (line.size() > 0) {
int num{-1};
// This is just to parse a number, nothing special here
if (line.find(",") != std::string::npos) {
num = std::stoi(line.substr(0, line.find(",")));
line.erase(0, line.find(",") + 1); // +1 to also delete delimiter
} else {
num = std::stoi(line);
line = ""; // Set line empty to go out of the while
}
tmp[col] = num;
col++;
}
// Assign the temporal vector to a row of the matrix
matrix[row] = tmp;
row++;
}
return {row, col};
}
std::cout << "Failed to open file at " << filename << std::endl;
return {-1, -1};
}
int main() {
int **matrix;
auto shape = readFile("file.csv", matrix);
for (size_t row = 0; row < shape.first; row++) {
for (size_t col = 0; col < shape.second; col++) {
std::cout << matrix[row][col] << " ";
}
std::cout << std::endl;
}
// Free the pointers
for (size_t row = 0; row < shape.first; row++) {
free(matrix[row]);
}
}
我目前的结果是:
0 0 -751362032 21853 1
2 3 4 2 3
2 1 3 4 5
3 2 1 4 3
2 2 2 2 2
free(): double free detected in tcache 2
Aborted (core dumped)
似乎在打印之前释放了第一个 tmp 向量(其余值是正确的)。知道我错过了什么吗?
您将 matrix
传递给未初始化的 readFile
函数,然后继续使用 matrix[i]=tmp
访问它。这可能会导致各种问题,因为您正在使用不属于您的内存。
我想用 CSV 中的值填充双指针二维数组。我不想在填充之前读取 csv 文件来获取数组的大小,我想用指针而不是 std::vector 来完成它。我当前的代码是这样的
std::pair<int, int> readFile(const std::string &filename, int **matrix) {
std::fstream file{filename, std::ios::in};
if (file.is_open()) {
std::string line{};
int col{0};
int row{0};
while (std::getline(file, line)) {
// Check how many cols there are
int len = std::count(begin(line), end(line), ',') + 1;
// Allocate a vector with size the cols found before
int *tmp = reinterpret_cast<int *>(calloc(len, sizeof(int)));
col = 0;
// Fill the temp vector with the read numbers
while (line.size() > 0) {
int num{-1};
// This is just to parse a number, nothing special here
if (line.find(",") != std::string::npos) {
num = std::stoi(line.substr(0, line.find(",")));
line.erase(0, line.find(",") + 1); // +1 to also delete delimiter
} else {
num = std::stoi(line);
line = ""; // Set line empty to go out of the while
}
tmp[col] = num;
col++;
}
// Assign the temporal vector to a row of the matrix
matrix[row] = tmp;
row++;
}
return {row, col};
}
std::cout << "Failed to open file at " << filename << std::endl;
return {-1, -1};
}
int main() {
int **matrix;
auto shape = readFile("file.csv", matrix);
for (size_t row = 0; row < shape.first; row++) {
for (size_t col = 0; col < shape.second; col++) {
std::cout << matrix[row][col] << " ";
}
std::cout << std::endl;
}
// Free the pointers
for (size_t row = 0; row < shape.first; row++) {
free(matrix[row]);
}
}
我目前的结果是:
0 0 -751362032 21853 1
2 3 4 2 3
2 1 3 4 5
3 2 1 4 3
2 2 2 2 2
free(): double free detected in tcache 2
Aborted (core dumped)
似乎在打印之前释放了第一个 tmp 向量(其余值是正确的)。知道我错过了什么吗?
您将 matrix
传递给未初始化的 readFile
函数,然后继续使用 matrix[i]=tmp
访问它。这可能会导致各种问题,因为您正在使用不属于您的内存。