C++ - deletion of pointer to array
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.
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.
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.
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.
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...
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...
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.
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.
I can't spot any errors in the part that you have posted.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement