   C++ Pointer help with a 2d array
  Hello all,So, once again going back to an old project. I am making some changes to an old maze program we did in college. Previously we assumed a size for the maze. So, in the code snippet below the mazeCell pointer array was initialized as a 6x6 array. The change I am trying to make to it is to allow a random size maze that will be input by the user or possibly just a random number generator.My question then, is in the header file, how do you go about declaring a 2-d array of pointers when you are not sure what the size is at build time and can only tell at run-time? This is what I have tried:private:MazeCell * mazeCell[][]; //Doesnt work//Then tried this   MazeCell * mazeCell[int][int]; //Doesnt workWhat exactly am I missing/forgetting?Thanks!Shawn   
Quote:Original post by shawnreWhat exactly am I missing/forgetting?When declaring arrays, the size of the element type must be known at compile time. For multidimensional arrays, that means that all but the first dimension must be specified in size.But since you're using C++, you are probably better off with std::vector<std::vector<MazeCell*> > or something similar.   ;Quote:Original post by shawnreMy question then, is in the header file, how do you go about declaring a 2-d array of pointers when you are not sure what the size is at build time and can only tell at run-time?You can't allocate beforehand when you don't know the size beforehand. You'll have to dynamically allocate it at run-time. In C++, you use the 'new' and 'delete' keywords for that (and for arrays of objects, there's the 'new []' and 'delete []' versions).MazeCell* cells;int count = 10;// Dynamically allocate 10 (or any other number) of MazeCell instancescells = new MazeCell()[count];// Do stuff with 'cells', and after you're done with it, make sure to clean them up:delete [] cells;You can go for a 2D array, but that's likely just complicating matters - using a 1D array and writing some interface functions (such as getCell(x, y)) is more usable in the long run.However, C++ offers you it's standard library, that contains container classes that can handle the dynamic allocation for you under the hood. Be sure to look into classes such as std::vector, std::deque and others. It's useful to be able to implement your own data structures, but most of the time the standard library already provides what you need, which will save you a lot of time. Invest some time in learning how to use it well, it'll pay off. :)It's usually also safer to use the standard library. You should be aware of certain things though, for example std::vector's behavior when it needs to resize itself. Different containers are useful in different situations.   ;  For a 2D array like so :  array2d[][], you have to specify the columnlike so : aray2D[][10]. The compiler needs to know how many column to create before hand.so you might want to try this :const int ROW = 10;const int COL = 10;MazeCell * mazeCell[ROW][COL];or just use vectors :)   ;thing** grid;grid = new thing*[verticalSize];for (int i = 0; i < verticalSize; i++)   {   grid = new thing[horizontalSize];   }This should let you dictate a dynamically sized 2D array of things....I think.  I haven't built a dynamic 2d array in a while.The only caveat is that you'll have to delete[] through a similar loop to clean up properly.   ;  I'm still living in the days of malloc!!declare: MazeCell *cells;..allocate when you know your random dimensions: cells = (MazeCell *)malloc(sizeof(MazeCell) * mazewidth * mazeheight);...index when needed: unsigned int index; index = x + y * mazewidth; cells[index];  is your cell at x and y coordinates... that should be fine. I am pretty positive that you don't need to define cells with brackets to tell the compiler that it's an array. hope that helps!;  malloc? Come on people, this is C++ we're talking about ;)#include <algorithm>#include <vector>#include <boost/checked_delete.hpp>#include <boost/utility.hpp>class Maze : boost::noncopyable{    int width;    std::vector<MazeCell*> maze;public:    Maze(int height, int width) :   width(width),   maze(height * width, static_cast<MazeCell*>(0)) {}    // if the maze owns the pointed-to cells,    // we have to delete them in the destructor    ~Maze()    {   std::for_each(maze.begin(), maze.end(), boost::checked_deleter<MazeCell>());    }    MazeCell** operator[](size_t y)    {   return &maze[y * width];    }};You can use Maze like this:Maze myMaze(25, 40);myMaze[0][0] = new MazeCell(whatever);;  Guys, you've all missed the mark.Quote:Original post by DevFredmalloc? Come on people, this is C++ we're talking about ;)You're closest; you even mention Boost in your code.But what the OP really wants - for a multi-dimensional, sized at runtime, but rectangular array - is boost::multi_array.   