Jump to content
  • Advertisement
Sign in to follow this  
Sir Sapo

Deleting Pointers

This topic is 4891 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

When I use the delete command to get rid of pointer objects, does it return all of the memory that the pointer took up back to the system, or does it just stop the pointer from being accessed? Also, if I have a pointer to an array: Player *players[100]; and I said: delete players; would it delete all the members of the array?

Share this post


Link to post
Share on other sites
Advertisement
The pointer itself takes up one int and you only get that memory back when the pointer is out of scope(local variable)/objected is deleted(member variable)/program ends(global variable).

When you use the delete operator it calls the destructor of the class you are deleting and then frees the memory that the instance of that class was using. You can still access that memory using the pointer, but you are no longer guaranteed that someone else will not write something there (basically you shouldn’t).

When you crate an array using new[] you should use delete[]. "delete players" will make a mess, since by the way it looks the memory was not dynamicly alocated.

Hope this helps, most of this stuff you can find in a C++ book. I'd recommend Effective C++ if you haven't read it yet.

Share this post


Link to post
Share on other sites
What you have corrupts either the free-store or the stack or perhaps both (nasty, nasty, bug).

Either

Player* players = new Player[100];
//...
delete[] players;

or

Player* players[100] = {0};
for(int i=0; i<100; ++i)
players = new Player;
//..
for(int i=0; i<100; ++i)
delete players;


If you use a new with a [] you need to match it with a delete[].

Share this post


Link to post
Share on other sites
Quote:
Original post by Magmai Kai Holmlor
What you have corrupts either the free-store or the stack or perhaps both (nasty, nasty, bug).

Either

Player* players = new Player[100];
//...
delete[] players;

or

Player* players[100] = {0};
for(int i=0; i<100; ++i)
players = new Player;
//..
for(int i=0; i<100; ++i)
delete players;


If you use a new with a [] you need to match it with a delete[].



You can add in a

if(players == NULL){} // this makes sure it is not NULL to begin with
delete players;
players=NULL; // set it to NULL after delete to be sure.

Or simply put this in a macro [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by DevLiquidKnight
You can add in a

if(players == NULL){} // this makes sure it is not NULL to begin with
delete players;
players=NULL; // set it to NULL after delete to be sure.

Or simply put this in a macro [smile]


That is redudndant, pointers passed to delete operators are permitted to be 0/null.

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!