deleting pointers
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
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 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.
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 :)
delete ptr;
Yeah slowwwww :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement