Jump to content
  • Advertisement
Sign in to follow this  
papa

Delete array of pointers - confused

This topic is 5469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am always confused how to delete dynamically alloc array holding a list of pointers each slot. Consider this example
class A
{
public:
	A(){ particles=NULL;}
	~A(){ if(particles) delete [] particles; }
	int *particles;
};

class B
{
public:
	B(){ objects=NULL; }
	~B(){ if(objects)delete [] objects;  }
	A *objects;
};

void main()
{
	B *b = NULL;
	b = new B[2];

	for(int i=0; i<2; i++)
	{
		b.objects = new A[4];
	}

	delete [] b; // Is it enough? }


The above delete [] b should call B's destructor and delete entire array holding 2 A objects. When each object A is deleted it should call its own constructor a delete its own array of particles (size 4) Is it right? or I need to loop through b and delete first its elements then delete b? Please let me know. thanks. [Edited by - papa on September 27, 2004 11:46:29 AM]

Share this post


Link to post
Share on other sites
Advertisement
Yes, that is fine. And you know, if you had written a simple test case, ie:


#include <iostream>

class A
{
public:
A(){ particles=NULL;}
~A(){
std::cout<<"A Destructor"<<std::endl;
if(particles) delete [] particles;
}
int *particles;
};

class B
{
public:
B(){ objects=NULL; }
~B(){
std::cout<<"B Destructor"<<std::endl;
if(objects)delete [] objects;
}
A *objects;
};

void main()
{
B *b = NULL;
b = new B[2];

for(int i=0; i<2; i++)
{
b->objects = new A[4];
}

delete [] b; // Is it enough?
}



you could have found this out on your own.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Yes, that is fine. And you know, if you had written a simple test case, ie:

*** Source Snippet Removed ***

you could have found this out on your own.


Well, it should be better to have


for(int i=0; i<2; i++)
{
b.objects = new A[4];
}


And this is not totally correct. When you call "delete [] b" you call the B destructor twice. This will delete two A arrays and therefore will call the destructor of A 2x4 times. The particles arrays are never allocated and thus, never destroyed.

Anyway, without the obvious memory leak I pointed out, you code is legal and seems to do what you expect (although I'm not sure wether you wrongly described what you think or you accuratly described something wrong).

Regards,


Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
And this is not totally correct. When you call "delete [] b" you call the B destructor twice. This will delete two A arrays and therefore will call the destructor of A 2x4 times. The particles arrays are never allocated and thus, never destroyed.

Yes, I wasn't paying attention and didn't see his pointer goof.

Share this post


Link to post
Share on other sites
oupsss. I think I gave you wrong example. see my corrected example.



class A
{
public:
A(){}
~A(){}
int particles;
};

class B
{
public:
B(){ objects=NULL; }
~B(){ if(objects)delete [] objects; }
A *objects;
};

void main()
{
B *b = NULL;
b = new B[2];

for(int i=0; i<2; i++)
{
b.objects = new A[4];
}

delete [] b; // Is it enough? }
}




So this will delete b array (size=2) and its internal objects array (size=4) for each b. There wont be definetely any memory leaks?

Share this post


Link to post
Share on other sites
Quote:
Original post by papa
oupsss. I think I gave you wrong example. see my corrected example.


*** Source Snippet Removed ***

So this will delete b array (size=2) and its internal objects array (size=4) for each b. There wont be definetely any memory leaks?


This is correct :)

Regards,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!