Jump to content
  • Advertisement
Sign in to follow this  
MTclip

will this be ok? -- pointer related

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

is this ok to do... addEntity() will copy tempEntity to a pointer... I know that the pointer in addEntity deleted.... will this in turn free the memory for tempEntity...
function(..stuff..)
{
	CEntity * tempEntity
	//GRAPHICAL
    if( type == 1 ) 
	{
		tempEntity = new CGraphical(xPos, yPos);
		earth->addEntity(tempEntity)
	};

    return
}

thanks....

Share this post


Link to post
Share on other sites
Advertisement
Assuming C++

If you do something like this:

void CEarth::addEntity(CEntity* entity) {
entityList.add(entity);
}
// destructor
void CEarth::~CEarth() {
deleteEntityListData(); // go through the list and delete everything
deleteOtherEarthlyData();
}


then it's ok, since your function allocates the memeory and hands the deletion over too someone else(in this case the earth).

Share this post


Link to post
Share on other sites
The value contained in variable being deleted is what counts, not the variable itself. So, this works:
    Foo * p1;
Foo * p2;
Foo * p3;

p1 = new Foo;
p2 = p1;
p3 = p2;

delete p3; // This is ok

Share this post


Link to post
Share on other sites
tempEntity itself is a pointer and was allocated on the stack, so it will be deleted automatically. What you are worried about is what will happen to the new CGraphical(xPos, yPos) whose address you stored in tempEntity. It is a totally separate object from tempEntity. As long as you call delete with the address of the new CGraphical(xPos, yPos) as the parameter exactly once in the program, it doesn't matter what pointer the address was stored in. Just make sure that all pointers that store that address are set to NULL afterwards so that you don't try to use it after it was deleted or delete it twice.

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!