Hi,
is it possible to deallocate memory, then use that same pointer after the delete statement
e.g
Or is that pointer unavailable to use when its memory it points to is unallocatedCode:delete ptr; ptr = 0;
Many Thanks
-Alex
Hi,
is it possible to deallocate memory, then use that same pointer after the delete statement
e.g
Or is that pointer unavailable to use when its memory it points to is unallocatedCode:delete ptr; ptr = 0;
Many Thanks
-Alex
You cannot dereference null pointers, but you can assign the same pointer object to different memory, and then it becomes safe to dereference, use pointer arithmetic, et cetera.
so i can't delete the memory pointed to by say ptr and then allocate more memory using the same pointer (ptr)
No, you can do that. The point is that you have to make sure you allocate new memory before you use it again.
No, I just said you could do that. What you need to be careful of is
* that the pointer you want to use is still in scope when you want to use it.
* that you only direference or use pointer arithmatic while ptr contains an address.
when in doubt, compile and run it. Worse case scenario* is you have to reboot.
*that is unless you are doing hinky stuff that could dmg your system.
>when in doubt, compile and run it.
That won't really tell you much.
One guy posted code that didn't work in the C forums, when trying to compare two strings. When I pointed out his error, he wanted to know why another piece of code of his worked when he did it the same way. The program that he said worked produced the desired end results for him, although it was wrong since it was comparing memory addresses instead of C strings, although because of his compiler optomising the building of the string table, he didn't realize anything was wrong.
Case in point: Just because something compiles, runs, and appears to give you the correct answer.... it doesn't mean it's actually right.