Sign in to follow this  
mrtie_dye

deleting pointers

Recommended Posts

Does this accomplish the same thing as deleting a pointer? int *ptr; ptr = new int; ptr = NULL; //does this do the same thing as deleting it? Thanks for any help

Share this post


Link to post
Share on other sites
No, setting it to NULL will not deallocate the memory allocated by new. To be safe, you should usually delete then set it to NULL (deleting a pointer that's already been deleted can cause a crash, deleting NULL does nothing).

Share this post


Link to post
Share on other sites
No.

You have to pair each new with a delete. The code you posted causes memoryleak.

Share this post


Link to post
Share on other sites
No it does not. What that does is essentially to give you a memory leak (since you suddenly have allocated memory you probably does not know how to release). To release the memory allocated by new, you need to call delete.

So the statement "delete ptr" before you set it to NULL will cause the memory to be released.

Share this post


Link to post
Share on other sites
You're only setting the pointer to point at memory address 0, which means that you've just made a memory-leak! The real way to delete is to use delete like this:
delete ptr;

Yeah slowwwww :)

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