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

naxchange
  • 893
  • 1
  • 11
  • 24
  • So statements like `cout << "position 1" << endl;` are producing no output? – WhozCraig Feb 20 '13 at 03:41
  • cout << "position1" << endl; is producing output, but the second one "position2" is not!! – naxchange Feb 20 '13 at 03:42
  • Make sure _array[tempDim1][tempDim2].vectorName_ is valid. – Grigorii Chudnov Feb 20 '13 at 03: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. – WhozCraig Feb 20 '13 at 03:43
  • I'll post an addendum in a second – naxchange Feb 20 '13 at 03:45
  • Thanks for the update. And `tempDim1` and `tempDim2` are.. what? (when that line is about to exit?)? We now know they must be between 0..1 and 1..2 respectively. Also, assuming they are, is the `vectorName` (which shouldn't even be dynamic in the first place) associated with that array entry also non-null? – WhozCraig Feb 20 '13 at 03:55
  • Added the missing info. Any suggestions? – naxchange Feb 20 '13 at 03:55
  • Actually, i take that back. That is your problem. You said `tempDim1` and `tempDim2` are 2, and 3, respectively? That means the line we've been pointing to all along is access beyond-allowable indexes in *both* dimensions. allowable indexing is [0..1][0..2] with the 2/3 limits you specified. – WhozCraig Feb 20 '13 at 03:57
  • Hmm, no the problem isn't indices. I just gave them the same name when I copied the code over here. – naxchange Feb 20 '13 at 03:59
  • Among several, yes. You're also accessing memory outside your array. The allowable indexing of your array is [0..1][0..2], you're passing [2][3], which is overreaching in *both* indexes. That you also haven't initialized your vector is another issue entirely (and one that could be entirely avoided by **not using a pointers in that structure**. just use `std::vector`. – WhozCraig Feb 20 '13 at 04:01
  • ok lemme do that one second – naxchange Feb 20 '13 at 04:04
  • EUREKA! It worked. Can anyone tell me why it is not advisable to use pointers to vectors? – naxchange Feb 20 '13 at 04:05

2 Answers2

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];
}
Pinch
  • 2,768
  • 3
  • 28
  • 44
  • 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? – naxchange Feb 20 '13 at 04:06
  • 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. – Pinch Feb 20 '13 at 04:08
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.