Thread: Dynamic array not working

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    Dynamic array not working

    I have used some info from a previous thread. I still can't get it to work, I want to make a 5 by 5 dynamically allocated array. The reason it is dynamic, because in the finished program the dimensions will e specified by the user. Anyhow here is the code:

    Code:
    #ifndef _GRIDWORLD_H
    #define _GRIDWORLD_H
    
    #include "DLList.h"
    #include "personarray.h"
    
    class CGridworld
    {
      public:
    
        CGridworld(int nrows=5, int ncols=5);//constructor with gridsize
        //~CGridworld();
    
        void move(int pid, int new_row, int new_col); //move person pid to 
    						  //  (new_row,new_col)
        void printMembers(int i, int j);	//print the people in district (i, j)
    
      private:
    
        int **Grid;			//two-dimentional array
        int nrows;			//number of rows in this grid world
        int ncols;			//number of columns in this grid world
        //CPersonArray P;	//stores person info
    };
    
    #endif
    
    //============GRIDWORLD.CPP
    #include <iostream>
    using namespace std;
    #include "gridworld.h"
    
    
    CGridworld::CGridworld( int nrows, int ncols)
    {
    	Grid = new int *[nrows];
    
    	for( int i = 1; i < nrows; i++){
    		Grid[i] = new int[ncols];
    	}
    
    	for(int j = 1; j < nrows; j++){
    		Grid[j] = 0; //initializing the whole grid to zero
    		cout << Grid[j] << endl;
    	}
    }

    right now the code gives me 8 cols and 5 rows
    any ideas,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    	for(int j = 1; j < nrows; j++){
    		Grid[j] = 0; //initializing the whole grid to zero
    		cout << Grid[j] << endl;
    	}
    Herein lies a problem. Grid[j] is an int*, remember. So what you are doing is, after allocating a bunch of arrays pointed to by these pointers, you set all of the pointers to null, thereby leaking memory. Additionally, arrays are 0-based.

    You need a nested loop:
    Code:
    for(int j = 0; j < nrows; j++){
      for(int k = 0; k < ncols; k++){
        Grid[j][k] = 0; //initializing the whole grid to zero
        cout << Grid[j][k] << endl;
      }
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    ok, that does work, thanks very much, but I am still left with only 20 elements, where I should have 25 with rows and cols being set to 5 each

    axon

    //EDIT:

    actually 24 with i=0.

    //EDIT2:

    everything is fine now...THANKS!
    Last edited by axon; 09-21-2003 at 11:09 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM