programcrashes when I resize array the 2nd time

This code is inside the constructor to a class. When I run my program it crashes and this is the output before it does so:
no problem here
or here
right here
this 0 0
0 output here
j: 0 k: 0 save[j][k]: 0
this 0 1
0 output here
j: 0 k: 1 save[j][k]: 17
at end of while loop
no problem here
or here
right here
this 0 0
0 output here
>Exit code: -1073741819


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//seed generator for latter use
    srand(std::time(0));
    properTime = 0;
    rows = 1;
    obstacles = new unsigned int *[rows];
    unsigned int i = 0;
    //memory allocated for  elements of each column.
    
    for(unsigned int i = 0 ; i < rows ; i++)
        obstacles[i] = new unsigned int[COLUMNS];
    unsigned int timeOfCaging = whenCaged();
    while(properTime < SECONDS_WORKED)
    {
        std::cout<<"no problem here"<<std::endl;
        timeOfCaging = Drib::whenCaged();
        obstacles[i][0] = timeOfCaging;
        obstacles[i][1] = Drib::GetRandomNumber(0, TREE_HEIGHT-1);
        std::cout<<"or here"<<std::endl;
        //resize
        /
        save = new unsigned int *[rows++];//increases the rows by 1
        std::cout<<"right here"<<std::endl;
        for(unsigned int j = 0; j < rows-1; j++){
            for(unsigned int k = 0; k < COLUMNS; k++){//columns is equal to 2
                std::cout<<"this "<<j<<" "<<k<<std::endl;
                std::cout<<obstacles[0][0]<<" output here"<<std::endl;
                save[j][k] = obstacles[j][k];
                std::cout<<"j: "<<j<<" k: "<<k<<" save[j][k]: "<<save[j][k]<<std::endl;
            }
        }
        //rows++;
        delete [] obstacles;
        obstacles = save;
        
        properTime += whenCaged();
        i++;
        std::cout<<"at end of while loop"<<std::endl;
    }
    std::cout<<"exit here"<<std::endl;


so the programing crashes on the third itteration of the while loop. I am using Scite
It looks like you have rows be 1, allocating the 2-d array with only one as the first dimension. i = 1 after the first for loop, then inside the while loop you access obstacles[i] (obstacles[1]) which is out of bounds. That looks like a problem to me, unless I'm misreading this.
Topic archived. No new replies allowed.