Thread: arrays

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    29

    arrays

    i'm making a class for a maze generator, but i ran into problems even from the beginning.

    i have a int named grid, which should be a two dimensional array. this one will store different values, which after bit analysis, will determine which walls are up or down in each square.

    i don't know how big each dimension will be. basically, all i want is setting the boundries of an array in the constructor of a class, via two variables, passed to the constructor. how can i do this?

    for example, if the user will call

    MazeClass MyMaze(10,15)

    the constructor should make a

    int grid[wid][hgt]; // where wid and hgt are passed to the constructor

    anyone knows? ty
    Last edited by izuael; 11-22-2006 at 08:20 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For grid[n1][n2],

    Code:
      int** grid = new int*[n1];
      for (int i=0; i != n1; i++) grid[i] = new int[n2];
    Edit: Changed n2 in the loop to n1.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    hmm..
    and what should i write in it's declaration (header), where i'm declaring grid protected?
    it's currently written int grid[][1];

    and although i set each item of the grid to 15 in the constructor,

    cout << a << ", " << b << ": " << grid[a][b] << "\n"

    gives me really odd results, where a and b are iterating from 0 to wid and form 0 to hgt

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Just declare it as
    Code:
      int **grid;
    In the constructor,
    Code:
      grid = new int*[n1];
      for (int i=0; i != n1; i++) grid[i] = new int[n2];
    In the destructor,
    Code:
      for (int i=0; i != n1; i++) delete[] grid[i];
      delete[] grid;

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Or, if you want to minimize cache misses,
    Code:
      grid = new int*[n1];
      grid[0] = new int[n1*n2];
      for (int i=1; i != n1; i++) grid[i] = grid[0] + i*n2;
    Edit: And the destructor is simpler then:
    Code:
      delete[] grid[0];
      delete[] grid;
    Edit: Another advantage of this method is that system calls like new and delete are expensive, and you make fewer of them this way, and avoid fragmenting your memory.
    Last edited by robatino; 11-22-2006 at 09:00 PM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    um, i don't get it
    this is what i made till now

    implementation:
    Code:
    CMaze::CMaze(int mwid, int mhgt)
    {
        wid=mwid;
        hgt=mhgt;
    
        int** grid = new int*[wid];
        for (int i=0; i < wid; i++) {grid[i] = new int[hgt];}
    
        for (int a = 0; a < wid; a++)
        {
            for (int b = 0; b < hgt; b++)
                {
                  grid[a][b]=15;	                           	  
                }
            }
    }
    
    CMaze::~CMaze()
    {
        for (int i=0; i != wid; i++) {delete[] grid[i];}
        delete[] grid;			   
    }
    
    void CMaze::printSize()
    {
      cout << wid << " x " << hgt << "\n";
    }
    
    void CMaze::ite()
    {
        for (int a = 0; a < wid; a++)
        {
            for (int b = 0; b < hgt; b++)
                {
                  cout << a << ", " << b << ": " << grid[a][b] << "\n";	  
                }	 	  
            }
    }
    declaration:
    Code:
    class CMaze
    {
    public:
          CMaze(int mwid, int mhgt);
    	  ~CMaze();
    	  void printSize();
    	  void ite();
    protected:
          int hgt;
          int wid;
          int **grid; 
    };
    whenever i am calling ite in my object from the main program, it crashes at 1,0.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    interestingly enough, the last example works.
    thanks.

    but if you do have a bit of time, can you explain me why it works?

    i really appreciate it.
    thanks again.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I'm worried that the first method doesn't work - they should both work. We're missing some bug. Maybe the bug affects the second method also, and you were just lucky in not seeing the crash. Anyway, the way the second method works is that if you want to have the entire maze allocated in a single contiguous chunk of memory, then grid[0] should point to its beginning, grid[1] should be equal to grid[0] + n2, grid[2] should be equal to grid[0] + 2*n2, etc. Each grid[i] is type int*, so grid is type int**, and you need to allocate n1 elements for grid to hold grid[0], grid[1], ..., grid[n1-1]. Then you allocate n1*n2 elements for the big array, set the beginning of it to grid[0], and then set the rest of the grid[i].

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    oh, now i see it.
    it's declaring an array inside of an array, exactly what i want, and doing all the memory calculations. mrr, this memory allocation thingie is really evil

    but i was expecting something like grid[a][b] i guess i'm doing this manually.

    about the first method.. i don't know what could it be. i'm using dev-c++, and it's been a source of problems since i installed it, but vc++ is horrible, as far as my first experience with it. could it be because of mingw?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I noticed that in your Constructor code above, you redefine grid, so it's not the same grid as the one in the class definition (I think):

    Code:
      int** grid = new int*[wid];
    Try

    Code:
      grid = new int*[wid];
    instead.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your class will probably crash if copied, you have to add a copy constructor and copy assignment operator. A better solution, though would be to use a vector (or a vector of vectors) that copies and destructs itself automatically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM