C++ - deletion of pointer to array

Started by
22 comments, last by tinu 15 years, 12 months ago
Ok, how about the entire CCoin and CPlayer classes?
Advertisement
class CPlayer
{
private:
CCoin *coinList;
public:
CPlayer();
void Uninitialise();
}


CPlayer::CPlayer()
{
coinList = new CCoin[4];
for(int i=0; i<4; i++)
{
coinList.setCoinID(i+1);
}
}
void CPlayer::Uninitialise()
{
if( this->coinList != NULL )
{
delete [] this->coinList;
this->coinList = NULL;
}
}
}


class CCoin
{

public:
CCoin();
~CCoin(); //empty one
int m_icoinID;
void setCoinID(int coinId);
};

void CCoin::setCoinID(int coinId)
{
m_icoinID = coinId;
}
Quote:Original post by tinu
just the standard Microsoft Error report warning...
not any particular one...


Ah okay, so the program crashes? I didn't catch that. Does it also crash when you run your program from inside Visual C++ ? I think normally the debugger should pause your program and tell you an error message (Fx. "Access violation reading ...") and point at the line where it happened. But this doesn't happen when you run it inside Visual C++ ?
Ya it breaks at point.
I have shown that in the initial post where it breaks
But it does not give any error message
Ya it breaks at point.
I have shown that in the initial post where it breaks
But it does not give any error message
Did you implement the copy constructor and assignment operators like I said in my first post?

EDIT: for an explanation of their importance see here, this only explains copy constructors but the assignment operator is important for the same reason.
no..
I dont know about dat...

Why should I implement those things?
How is it connected to the current problem
See the link in my edit of the previous post. It explains the problem fairly well.
Quote:Original post by tinu
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.


If you don't mind making a few changes to your code, why not use vectors and auto_ptrs instead of arrays and raw pointers? These were features kept around in C++ for C compatibility; C++ has much safer, and standardized, ways of dealing with these problems.

Unless you really can't, always use the C++ standard classes instead of C-style solutions. You'll save yourself a lot of trouble.
okay I will look into it...
And try those and come with the results...
thanks for the replies..

This topic is closed to new replies.

Advertisement