Reputation: 2141
So I am having an issue within the two methods posted below. Aside from terrible naming of variables, I only have one problem. Somehow between getData and cutRamping, my vector TwoDee disappears. I have traced it using gdb, and at the end of getData it is filled with values, but when I try to trace it manually or the program tries to use it in cutRamping it throws a segfault relating to the iterator in the vector, and when I tell gdb to print the vector, it prints 0x0 instead of a valid memory address. These two methods are literally called back to back, so I don't know how I could be losing the data in the vector TwoDee without me calling any methods that would do that. Any help would be appreciated!
header file:
class Deviation
{
public:
Deviation();
void graphDev(std::string infile, std::string outfile1, std::string outfile2, std::vector<double> plats);
private:
void getData();
void cutRamping();
void calcDev();
void makeGraph();
std::string input, output1, output2;
std::vector<double> current, dx, dy, gcurrent, gdx, gdy, gdevx, gdevy, tempcurrent, tempdx, tempdy, plateaus;
std::vector< std::vector<double> > TwoDee, FileOut, FileOutTemp; // File Out is a 2-D array that holds the Strength, b3-6 and a3-6 that are to be written to the file in that order where the Temp just holds each plateau value
std::vector<double> curRow;
};
cpp file:
void Deviation::getData()
{
float f;
vector< vector<float> > TwoDee;
vector <float> curRow;
TwoDee.clear();
curRow.clear();
FILE * pFile;
ifstream inFile(input.c_str());
// Counts the number of lines in the file
unsigned int lines = count(istreambuf_iterator<char>(inFile), istreambuf_iterator<char>(), '\n');
inFile.close();
pFile = fopen (input.c_str(),"r");
// Pushes all of the data into a 2-D vector
for (unsigned int j = 0; j<lines; j++)
{
for (unsigned int i = 0; i<37; i++)
{
fscanf (pFile, "%f", &f);
curRow.push_back((double)f);
// Just says that it pushed back a value
//cout<<"just pushed back "<<f<<endl;
}
TwoDee.push_back(curRow);
curRow.clear();
}
fclose (pFile);
for (unsigned int i = 0; i<TwoDee[0].size(); i++)
{
current.push_back(TwoDee[1][i]);
dx.push_back(TwoDee[4][i]);
dy.push_back(TwoDee[5][i]);
}
}
void Deviation::cutRamping()
{
// Moved these out of the loop for now
tempcurrent.clear();
tempdx.clear();
tempdy.clear();
unsigned int i = 0;
bool keepgoing = true;
for (unsigned int j = 0; j<plateaus.size(); j++)
{
keepgoing = true;
while (i<current.size()&&keepgoing)
{
if (fabs(plateaus[j]-current[i]) < 0.2 && i<current.size()) // (0.2 is the tolerance to determine if the current is ramping or not)
{
tempcurrent.push_back(current[i]);
tempdx.push_back(dx[i]);
tempdy.push_back(dy[i]);
FileOutTemp[0].push_back(TwoDee[3][i]);
// ...
// Rest of the method that never gets called
}
Upvotes: 0
Views: 1235
Reputation: 96251
The vector in getData
is "shadowing" (hiding) the vector in your class, and then when getData
exits, all the contents of the vector are freed. You should probably just not redeclare it in the method.
Upvotes: 2