-
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
-
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.
-
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
-
Just declare it as
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;
-
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.
-
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.
-
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.
-
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].
-
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?
-
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.
-
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.