CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    167

    How to identify and release dynamic memory

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: How to identify and release dynamic memory

    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

  3. #3
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    167

    Re: How to identify and release dynamic memory

    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

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: How to identify and release dynamic memory

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured