C
countofquad
What I need is a 2D array of pointers to instances of class foo.
Here is a simplified example of what I do to allocate the memory.
class foo
{
public:
int x;
int y;
};
// example 2d array
int x_max = 10;
int y_max = 10;
foo ***map;
map = new (foo **) [x_max];
for(int x = 0; x < x_max; x++) {
map[x] = new (foo *) [y_max];
}
for(int y = 0; y < y_max; y++) {
for(int x = 0; x < x_max; x++) {
map[x][y] = new foo();
}
}
But I have no idea how to free the memory once I'm done using map.
I tried using:
for(int x = 0; x < x_max; x++) {
delete [] map[x];
}
delete map;
But this doesn't seem to free it up. I've been wringing my hair for a
couple of hours trying to figure this out, so many thanks to those who
can help me with this.
Shaun
Here is a simplified example of what I do to allocate the memory.
class foo
{
public:
int x;
int y;
};
// example 2d array
int x_max = 10;
int y_max = 10;
foo ***map;
map = new (foo **) [x_max];
for(int x = 0; x < x_max; x++) {
map[x] = new (foo *) [y_max];
}
for(int y = 0; y < y_max; y++) {
for(int x = 0; x < x_max; x++) {
map[x][y] = new foo();
}
}
But I have no idea how to free the memory once I'm done using map.
I tried using:
for(int x = 0; x < x_max; x++) {
delete [] map[x];
}
delete map;
But this doesn't seem to free it up. I've been wringing my hair for a
couple of hours trying to figure this out, so many thanks to those who
can help me with this.
Shaun