Click to See Complete Forum and Search --> : How to identify and release dynamic memory


bagavathikumar
July 4th, 2007, 12:29 PM
dear friends
my doubts is how can i identify whether a pointer variable(dynamic variable) having allocated memory or not to free.
Because if you free a variable that is already released.


regards
bagavathikumar

ZuK
July 4th, 2007, 12:39 PM
You can't find out that information.
All you can do is set pointers to 0 after freeing so that a another call to delete/free would not have any effect ( it is allowed to call delete/free on a null pointer ).
But there could be multiple pointers to the same object and then ths approach would not work.
Kurt

bagavathikumar
July 4th, 2007, 01:37 PM
Thanks for your immediate reply.

I have a class with one Pointer variable. I wrote a method called SetData. It will get the Input from the user. if the value is 0 to 100, it will allocate a memory to pointer variable. So that i want to release the memory in destructor, if it is allocated. How can identify whether a pointer variable contain the allocated memory.

Thanks in advance
Bagavathikumar

ZuK
July 4th, 2007, 01:40 PM
In that case you only have to initialize that pointer to 0 in the constructor. Then it is alwais safe to delete that pointer in the destructor.
Kurt