deleting dynamically allocated multi dem arrays
Moderator: Coders of Rage
- WSPSNIPER
- Chaos Rift Regular
- Posts: 145
- Joined: Sun Jan 03, 2010 6:19 pm
- Current Project: top down shooter
- Favorite Gaming Platforms: ps3
- Programming Language of Choice: c++
deleting dynamically allocated multi dem arrays
i have a dynamically allocated multi dem array and i was wondering how to delete it
i heard either
delete [] tile and thats it or
i heard
for( i = 0; i < arraySize; i++)
{
delete [] tile;
tile = NULL;
}
and i dont want to do it wrong so it would be kind of someone to give me some help thanks
i heard either
delete [] tile and thats it or
i heard
for( i = 0; i < arraySize; i++)
{
delete [] tile;
tile = NULL;
}
and i dont want to do it wrong so it would be kind of someone to give me some help thanks
Re: deleting dynamically allocated multi dem arrays
what do you mean dynamically allocated? cause i think you might be mistaken.
could you show the code where you make the array?
could you show the code where you make the array?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: deleting dynamically allocated multi dem arrays
Creation
Deletion
Code: Select all
int** tiles;
tiles = new int*[columns];
for(int i = 0; i < columns; i++)
tiles[i] = new int[rows];
Code: Select all
for(int i = 0; i < columns; i++)
delete [] tiles[i];
delete [] tiles;
Re: deleting dynamically allocated multi dem arrays
Well, I didn't want to make a new post, since my question is like an extension off of this, but how would you deallocate a dynamic 3D array? I've been working on a simple map editor, and I'm trying to add a 3rd dimension in my array of tile for layers, but I'm getting an access violation trying to free the array.
I'm allocating like this:
And I'm a little lost on deallocating it. I've tried a few different ways, but I keep getting an access violation... Any help is much appreciated =D
I'm allocating like this:
Code: Select all
mLevel = new CTile**[m_iLayers];
for(int i = 0; i < m_iLayers; i++)
{
mLevel[i] = new CTile*[m_iWidth];
for(int j = 0; j < m_iWidth; j++)
{
mLevel[i][j] = new CTile[m_iHeight];
}
}
Re: deleting dynamically allocated multi dem arrays
why dont you declare it
CTile *mLevel[x][y][z]
for x
for y
for z
mLevel[x][y][z] = new CTile(formal parameters);
end
end
end
to delete i think it be something like
for x
for y
for z
delete mLevel[x][y][z];
end
end
end
CTile *mLevel[x][y][z]
for x
for y
for z
mLevel[x][y][z] = new CTile(formal parameters);
end
end
end
to delete i think it be something like
for x
for y
for z
delete mLevel[x][y][z];
end
end
end
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: deleting dynamically allocated multi dem arrays
avansc wrote:why dont you declare it
CTile *mLevel[x][y][z]
for x
for y
for z
mLevel[x][y][z] = new CTile(formal parameters);
end
end
end
to delete i think it be something like
for x
for y
for z
delete mLevel[x][y][z];
end
end
end
Uhh the tiles aren't dynamic, just the arrays, right 0.o? (In my code)
Re: deleting dynamically allocated multi dem arrays
XianForce wrote:avansc wrote:why dont you declare it
CTile *mLevel[x][y][z]
for x
for y
for z
mLevel[x][y][z] = new CTile(formal parameters);
end
end
end
to delete i think it be something like
for x
for y
for z
delete mLevel[x][y][z];
end
end
end
Uhh the tiles aren't dynamic, just the arrays, right 0.o? (In my code)
err what now.. you said a 3 dimensional array no?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: deleting dynamically allocated multi dem arrays
Yeah, but with my code, the tiles themselves aren't dynamically allocated, are they? (It's sort of a side question, because I'm curious)
Re: deleting dynamically allocated multi dem arrays
have you tried
delete [][] tiles;
ps: what is mLevel declared as? i mean structure wise, mot what type.
edit:
you would do it like this how you have your setup. (i think)
i just made it a true 3d array. so its a bit different from yours. but the delete should be very much the same.
delete [][] tiles;
ps: what is mLevel declared as? i mean structure wise, mot what type.
edit:
you would do it like this how you have your setup. (i think)
Code: Select all
#include <iostream>
#include <stdio.h>
class test
{
public:
test(int a);
~test();
private:
int a;
};
test::test(int a)
{
this->a = a;
}
test::~test()
{
}
#define SX 100
#define SY 100
#define SZ 100
int main(int argc, char * const argv[])
{
test ****bleh;
bleh = new test ***[SX];
for(int x = 0;x < SX;x++)
{
bleh[x] = new test **[SX];
for(int y = 0;y < SX;y++)
{
bleh[x][y] = new test *[SY];
for(int z = 0;z < SZ;z++)
{
bleh[x][y][z] = new test(x*y*z);
}
}
}
printf("created!\n");
getchar();
for(int x = 0;x < SX;x++)
{
for(int y = 0;y < SX;y++)
{
for(int z = 0;z < SZ;z++)
{
delete bleh[x][y][z];
}
delete [] bleh[x][y];
}
delete [] bleh[x];
}
delete [] bleh;
printf("deleted!\n");
getchar();
return 0;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: deleting dynamically allocated multi dem arrays
Yeah, mine has 3 levels of indirection, while yours has 4. So your creating a dynamically allocated 3D array of dynamically allocated objects, but the objects themselves in mine aren't dynamically allocated. But that example did help a lot! I'm not getting an access violation now, but I gotta check and see if it leaks still.
Thanks a ton for the help =D
Thanks a ton for the help =D