deleting pointers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: deleting pointers

  1. #1
    Join Date
    May 2004
    Posts
    242

    deleting pointers

    Hello,

    I have the following code in main:-

    int _tmain(int argc, _TCHAR* argv[])
    {
    MyAmi *ptr = 0;
    ptr->doIt();
    delete ptr; ptr = NULL;
    return 0;
    }
    A colleague of mine tells me that I don't have to do this: -
    delete ptr; ptr = NULL;
    becasue doIt() is the last thing that will take place by the compiler and ptr will be out of scope when main finishes, similarly to :-

    int _tmain(int argc, _TCHAR* argv[])
    {
    int i = 5;
    return 0;
    }
    i.e. we don't delete i (and ptr is just like any other variable and holding an address of another variable shouldn't make any difference).

    However, my experience tells me all pointers need to be deleted before the program finishes. He says that only if there are pointers member variables and thus, the constructor allocates memory using new, then the destructor need to deallocate that memory using delete.

    Can you please advise.

    Many thanks.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Both you and your colleague are wrong. First, your pointer is initialized to 0 and remains with that value. You can't call a member function, e.g., doIt, for a null pointer. The call will most likely cause a crash and terminate the program.
    Secondly, you can't use delete just by a hunch or gut feeling. The rules are very rigid here: if you've used new, you must use delete, and you haven't used new, you mustn't use delete.
    Finally (and without contradicting what I've just said), deleting a null pointer has no effect, so you certainly have no reason to delete such a pointer.
    Danny Kalev

  3. #3
    Join Date
    May 2004
    Posts
    242
    OK, thanks Danny for the clarification.

    I actually realised, my code should have read :-

    int main()
    {
    MyAmi *ptr = new MyAmi();
    ptr->doIt();
    delete ptr; ptr = NULL;

    return 0;
    }

    I still wonder how come I got the correct result without doing 'new'.

    Was I just lucky with the compiler ?

  4. #4
    Join Date
    May 2007
    Posts
    843
    I think you have get the point.

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    Is DoIt a static member function by any chance?
    And is the pointer null or not when you can that function?

    The compiler has nothing to do with the results you got; the program executes at runtime, not at compile-time. So whatever the results are, this is how the implementation and the operating system handle such code.
    As said before, if you used new, you should include a delete statement somewhere in the program after the new expression. Otherwise you're causing a memory leak.
    Danny Kalev

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2006, 02:58 AM
  2. How to get pointers to variables and file location
    By Benzsoft in forum VB Classic
    Replies: 1
    Last Post: 02-21-2002, 11:47 AM
  3. Re: Is C# secure ??? (Pointers?)
    By ceemar in forum .NET
    Replies: 4
    Last Post: 01-31-2001, 05:47 PM
  4. Deleting Excel Records with ADO
    By Saiful in forum VB Classic
    Replies: 1
    Last Post: 08-04-2000, 04:05 PM
  5. Re: Deleting a record with ADO .... HELP
    By Jeff Binnig in forum VB Classic
    Replies: 0
    Last Post: 03-27-2000, 04:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center