Sign in to follow this  
tinu

C++ - deletion of pointer to array

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[i].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
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
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
class CPlayer
{
private:
CCoin *coinList;
public:
CPlayer();
void Uninitialise();
}


CPlayer::CPlayer()
{
coinList = new CCoin[4];
for(int i=0; i<4; i++)
{
coinList[i].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;
}

Share this post


Link to post
Share on other sites
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++ ?

Share this post


Link to post
Share on other sites
Ya it breaks at point.
I have shown that in the initial post where it breaks
But it does not give any error message

Share this post


Link to post
Share on other sites
Ya it breaks at point.
I have shown that in the initial post where it breaks
But it does not give any error message

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
no..
I dont know about dat...

Why should I implement those things?
How is it connected to the current problem

Share this post


Link to post
Share on other sites
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[i].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.

Share this post


Link to post
Share on other sites
okay I will look into it...
And try those and come with the results...
thanks for the replies..

Share this post


Link to post
Share on other sites
They tell that copy constructors are used in three foll scenarios:

1. When an object is created from another object of the same type
2. When an object is passed by value as a parameter to a function
3. When an object is returned from a function

I am not using these scenarios in my program.
Any ways I will try implementing them.

And they dont state anything about assignment operators...

Share this post


Link to post
Share on other sites
Just do a google search on assignment operators in C++, you will get plenty of results. Even if their absence is not the current problem you are having, you will run into problems later if you don't have them.

Share this post


Link to post
Share on other sites
Quote:
Original post by tinu
They tell that copy constructors are used in three foll scenarios:

1. When an object is created from another object of the same type
2. When an object is passed by value as a parameter to a function
3. When an object is returned from a function

I am not using these scenarios in my program.
Any ways I will try implementing them.

And they dont state anything about assignment operators...


It's easy to inadvertently call a copy constructor or an assignment operator. If you don't mean to call them, declare private ones that don't do anything.

#include <iostream>

typedef double Pixel;

struct Image {
int width, height;
Pixel *data;

Image(int width, int height)
: width(width),
height(height),
data(new Pixel[width*height]){
}

void clear(Pixel p) {
int size = width*height;
for(int i=0;i<size;++i)
data[i] = p;
}

~Image() {
delete [] data;
}

private:
Image(Image &);
void operator=(Image &);
};

int image_size(Image i) { // <-- Copy constructor will be called!
return i.width * i.height;
}

int main() {
Image i(640,480);
std::cout << image_size(i) << '\n';
}



The compiler will not let you write this mistake because the copy constructor is private. If you remove the private section of the struct, you'll see that the program compiles fine but crashes in a similar fashion as yours.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this