Search code examples
c++vectorpush-back

vector.push_back


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


Solution

  • 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];
    }