Page 1 of 1

deleting dynamically allocated multi dem arrays

Posted: Sun Jan 31, 2010 11:27 am
by WSPSNIPER
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

Re: deleting dynamically allocated multi dem arrays

Posted: Sun Jan 31, 2010 1:32 pm
by avansc
what do you mean dynamically allocated? cause i think you might be mistaken.
could you show the code where you make the array?

Re: deleting dynamically allocated multi dem arrays

Posted: Sun Jan 31, 2010 2:40 pm
by Bakkon
Creation

Code: Select all

int** tiles;

tiles = new int*[columns];

for(int i = 0; i < columns; i++)
	tiles[i] = new int[rows];
Deletion

Code: Select all

for(int i = 0; i < columns; i++)
	delete [] tiles[i];

delete [] tiles;

Re: deleting dynamically allocated multi dem arrays

Posted: Sun Jan 31, 2010 3:25 pm
by WSPSNIPER
thanks

Re: deleting dynamically allocated multi dem arrays

Posted: Sun Feb 07, 2010 9:32 pm
by XianForce
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:

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];
                }
        }
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

Re: deleting dynamically allocated multi dem arrays

Posted: Sun Feb 07, 2010 10:32 pm
by avansc
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

Re: deleting dynamically allocated multi dem arrays

Posted: Sun Feb 07, 2010 10:47 pm
by XianForce
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

Posted: Sun Feb 07, 2010 11:10 pm
by avansc
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?

Re: deleting dynamically allocated multi dem arrays

Posted: Mon Feb 08, 2010 12:00 am
by XianForce
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

Posted: Mon Feb 08, 2010 9:47 am
by avansc
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)

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;
}
i just made it a true 3d array. so its a bit different from yours. but the delete should be very much the same.

Re: deleting dynamically allocated multi dem arrays

Posted: Mon Feb 08, 2010 1:50 pm
by XianForce
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