I am writing an application that reads from data files of a given format. In the file, I've dynamically created a 2D array of pointers to vector objects. Basically, it reads through the file, and when it finds a given string pattern, it stops and reads
while(getline(inputFile,tempTestString)){
// first for ACCEL
if(string::npos != tempTestString.find(ACCEL)){
sstream.str(tempTestString);
sstream >> pushBack;
cout << "position 1" << endl;
array[tempDim1][tempDim2].vectorName->push_back(pushBack);
cout << "position 2" << endl;
break;
}
}
now, pushBack is a large number, could be up to 20000, but it varies between files.
The problem with this code is that I'm not getting any run-time errors, or even any exceptions thrown, I tried catching them. The program simply finishes! To be sure, I added the cout << "position1" << endl; and cout << "position2" << endl; lines and the latter prints.
In case you haven't guessed:
tempTestString and ACCEL - string objects
sstream - stringstream object
array - 2D struct array in dynamic memory
vectorName - pointer to vector object, member of struct pointed to by array
ADDENDUM:
So, in response to some comments, here is the other portion of the code, where all the variables were created:
array
array = new structName* [tempDim1];
for(int i = 0; i < tempDim2; i++){
array[i] = new structName [tempDim2];
}
structName
struct structName{
vector<double>* vectorName;
vector<double>* vectorName1;
vector<double>* vectorName2;
};
tempDim1 and tempDim2 are both const ints, of values 2 and 3, respectively. pushBack can have a value of up to 20000
2 Answers 2
Try to correct this:
array = new structName* [tempDim1];
for(int i = 0; i < tempDim2; i++){
array[i] = new structName [tempDim2];
}
=>
array = new structName* [tempDim1];
for(int i = 0; i < tempDim1; i++){
array[i] = new structName [tempDim2];
}
2 Comments
You're using the wrong number of elements in your initialization.
array = new structName* [tempDim1];
for(int i = 0; i < tempDim2; i++){
array[i] = new structName [tempDim2];
}
i < tempDim2 is wrong; the array has tempDim1 elements.
I don't know if this is the problem, but it is a problem. If tempDim1> tempDim2 then some elements of array[] are going to be uninitialized. (And if it's the other way around, you're corrupting memory.) The only way this would work is if tempDim1 and tempDim2 are the same by coincidence.
cout << "position 1" << endl;are producing no output?array[tempDim1][tempDim2].vectorName->push_back(pushBack);as being the problem, and other than a brief description, none of it do we know anything about. Since you're printing acoutthere anyway, how about dumping all those indexes and the value ofpushBackjust for some clarity in your instrumentation.