deleting pointers

Started by
3 comments, last by MindFlayer 18 years, 8 months ago
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
Marriage is the #1 cause of divorce
Advertisement
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).
No.

You have to pair each new with a delete. The code you posted causes memoryleak.
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.
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 :)

This topic is closed to new replies.

Advertisement