3

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

asked Feb 20, 2013 at 3:40
12
  • So statements like cout << "position 1" << endl; are producing no output? Commented Feb 20, 2013 at 3:41
  • cout << "position1" << endl; is producing output, but the second one "position2" is not!! Commented Feb 20, 2013 at 3:42
  • Make sure array[tempDim1][tempDim2].vectorName is valid. Commented Feb 20, 2013 at 3:43
  • 2
    Which begs the question Grigoriy just pointed out? everything points to 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 a cout there anyway, how about dumping all those indexes and the value of pushBack just for some clarity in your instrumentation. Commented Feb 20, 2013 at 3:43
  • I'll post an addendum in a second Commented Feb 20, 2013 at 3:45

2 Answers 2

3

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];
}
answered Feb 20, 2013 at 4:00
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks guys. I made a mistake while I was copying the code, but when I removed the pointers to vectorName in my structs, it worked. Can anyone tell me why it didn't work though?
When there's an out of bounds error, the behavior of C++ program is undefined (unlike JAVA where it will throw an exception). You simply can't tell what will happen.
0

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.

answered Feb 20, 2013 at 4:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.