Jump to content
  • Advertisement
Sign in to follow this  
tinu

C++ - deletion of pointer to array

This topic is 3719 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 face difficulty in deleting pointers to arrays. I have a pointer to an array of player objects. m_aplayer = new CPlayer[4]; Each player object has a pointer to an array of coin objects. coinList = new CCoin[4]; (this is done in player constructor) Now I want to delete the pointer to that array of player objects. In an uninitialize function( member of player class ), I delete the pointer to an array of coin objects. void Uninitialise() { if( this->coinList != NULL ) { delete [] this->coinList; ----------------------> breaks in this line this->coinList = NULL; } } This is repeatedly done for all player objects. for( int i = 0; i <4; i++ ) { m_player.Uninitialise(); } Then I delete the pointer to array of player objects. delete[] m_player; Sometime the program works fine. and some times at the deletion of coin objects the application breaks. Cant understand the behaviour.

Share this post


Link to post
Share on other sites
Advertisement
Hi!

Did you try examining the contents of your coinlist right before you try to delete it? Are the values as expected? Does the CCoin destructor do anything?

Is there a reason you are using the cumbersome and error prone manual array allocation, instead of using the standard library containers?

Also, is there a reason you have a separate Uninitialize method instead of doing it in the destructor? (this isn't related to the problem per se, just asking)

EDIT: also, since you have raw pointers in your classes, make sure they have the appropriate copy constructors and assignment operators.

Share this post


Link to post
Share on other sites
I generally dislike giving unsolicited advice, but why not use a std::vector? Arrays can have their use, but this situation doesn't seem to require one.

Share this post


Link to post
Share on other sites
CCoin destructor does nothing.

I have provided a separate uninitialise function guessing that destructor is not the rite place to do them.

ya I accept that I can use the vectors in STL....

But still I want to know wat goes wrong here...

Share this post


Link to post
Share on other sites
just the standard Microsoft Error report warning...
not any particular one...

Share this post


Link to post
Share on other sites
Quote:
Original post by tinu
CCoin destructor does nothing.

I have provided a separate uninitialise function guessing that destructor is not the rite place to do them.

ya I accept that I can use the vectors in STL....

But still I want to know wat goes wrong here...


The destructor serves exactly for 'uninitializing' everything a class contains. So it's definitely the right place to do this.

Share this post


Link to post
Share on other sites
ok .
I was initially doing this in destructor only.
But problem persisted then too...

Share this post


Link to post
Share on other sites
How big is the whole program? Would it be feasible to post it all? (in [ source] [ / source] tags obviously)

I can't spot any errors in the part that you have posted.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!