Thread: getting confused with pointer to a pointer - 2d array

  1. #1
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13

    dynamic array crashing after 2nd time of expanding

    crashes at
    Code:
    unsigned int **save = new unsigned int *[rows+1];//increases the rows by 1
    and I don't know why.

    Code:
        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
           
            unsigned int **save = new unsigned int *[rows+1];//increases the rows by 1
            std::cout<<"right here"<<std::endl;
            for(unsigned int j = 0; j < rows; j++){
                for(unsigned int k = 0; k < COLUMNS; k++){//columns is equal to 2
                    std::cout<<"this"<<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;
    Last edited by help_seed; 12-03-2009 at 02:29 AM. Reason: remove extra commenting

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Return pointer to 2d array
    By albert3721 in forum C Programming
    Replies: 3
    Last Post: 10-22-2007, 04:30 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM