我的代码的这一部分的想法相对简单;我正在做一个遍历某个整数范围的for循环,迭代的值放在一个数字到字符串的转换中,然后在for循环中调用以读取一个数据文件。
代码段:
for (int z = 11; z < 33; z++) {
const std::string filename_1 = "detection";
int value = z;
std::string filename_2;
std::ostringstream convert;
convert << value;
filename_2 = convert.str();
const std::string filename_3 = ".rat";
const std::string my_file = filename_1 + filename_2 + filename_3;
// Reading data from the .rat files of the detected signal.
Reading(my_file, times_det, signal_det);
// Further calculations go here.
代码编译并执行,但在读出两个计算结果后,我返回错误:
./test
0.527679
0.639159
*** Error in `./test': double free or corruption (out): 0x0000000000f52a30 ***
Aborted (core dumped)
我是否在必要时清除记忆?使用.clear()并没有消除这个问题。
感谢您的建议!
编辑:为了清晰起见,“阅读”功能:
void Reading(const std::string &filename, rarray<double,1> ×, rarray<std::complex<double>,1> &signal) {
std::ifstream f(filename.c_str());
// Read in the signal.
f >> times;
f >> signal;
}
答案 0 :(得分:0)
@kam,我试图通过使用以下代码重现该问题,我怀疑它可能与您正在使用的rarray有关,但注释掉因为我没有库的详细信息正在使用。
#include <iostream>
#include <fstream>
#include <strstream>
#include <sstream>
using namespace std;
void Reading(const std::string &filename, /*rarray< */double /*,1> */ ×, /* rarray<std::complex< */ double /*>,1> */ &signal) {
std::ifstream f(filename.c_str());
// std::ifstream f;
std::string ofile = "o_" + filename;
std::ofstream o;
//f.open(filename.c_str());
o.open(ofile.c_str());
// Read in the signal.
f >> times;
f >> signal;
cout << "file open :" << f.is_open() << " ";
cout << times << " " << signal << endl << flush;
//o << "file open :" << f.is_open() << " ";
o << times << " " << signal << endl << flush;
//f.close();
o.close();
}
int main(int argc, const char * argv[]) {
const std::string filename_1 = "detection";
const std::string filename_3 = ".rat";
double times_det = 0.0, signal_det = 0.0;
for (int z = 11; z < 33; z++) {
int value = z;
std::string filename_2;
std::ostringstream convert;
convert << value;
filename_2 = convert.str();
const std::string my_file = filename_1 + filename_2 + filename_3;
// Reading data from the .rat files of the detected signal.
Reading(my_file, times_det, signal_det);
// Further calculations go here.
}
return 0;
}