Deleting Pointers?

This is a discussion on Deleting Pointers? within the C++ Programming forums, part of the General Programming Boards category; A) Say I have a pointer in my main, and I send that pointer to a function as an argument: ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Deleting Pointers?

    A) Say I have a pointer in my main, and I send that pointer to a function as an argument:

    Code:
    int main()
    {
       Object *myPtr;
       someFunction(myPtr);
    }
    In that function:

    Code:
    void someFunction(Object *somePtr)
    {
       somePtr = 0;
       delete somePtr;
    }
    Since it points to the same location in memory, does that delete the original object?

    -----

    B) How do I delete a pointer if it's a return type?

    Code:
    Object* someFunction()
    {
       AnotherObject* myPtr2;
       return myPtr2;
    }
    Is there a way to get that memory back for myPtr2?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
      1. You are not properly allocating memory.
      2. You are setting the pointer in someFunction() to 0 before deleting it, which means you are deallocating nothing.
      3. If you set somePtr to any value in someFunction(), such a change is not reflected in myPtr in main() (in terms of what the pointer value is.)
      4. Deleting memory in a function is allowed in the manner that you have (provided you fix the setting-to-0-first error). Such memory should not be accessed anywhere else after being deallocated.
      1. You're not allocating memory for myPtr2.
      2. You delete it in the calling function..... ie.
        Code:
        AnotherObject *o = someFunction();
        ...
        delete o;

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by MacGyver View Post
    1. You're not allocating memory for myPtr2.
    2. You delete it in the calling function..... ie.
      Code:
      AnotherObject *o = someFunction();
      ...
      delete o;
    1. Yeah I forgot the, "= new Object()" lines for this post, but I do it in my program

    2. Oh yeah, of course!

    Quote Originally Posted by MacGyver View Post
    1. You are not properly allocating memory.
    2. You are setting the pointer in someFunction() to 0 before deleting it, which means you are deallocating nothing.
    3. If you set somePtr to any value in someFunction(), such a change is not reflected in myPtr in main() (in terms of what the pointer value is.)
    4. Deleting memory in a function is allowed in the manner that you have (provided you fix the setting-to-0-first error). Such memory should not be accessed anywhere else after being deallocated.
    2. Hmm? Don't set to 0 first?

    The address in memory it's pointing to is different from the memory where the pointer actually is. So there's two steps to do, no? Change pointer->address, then delete the variable pointer completely?

    [Edit]: I just tried it in my program, and if I don't set it to 0 first, it seg faults immediately. Are you sure?
    Last edited by Paul22000; 07-24-2008 at 08:39 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I just tried it in my program, and if I don't set it to 0 first, it seg faults immediately. Are you sure?
    If the pointer is 0, then delete does nothing at all. You don't delete pointers, you delete what the pointer is pointing at.

    If it segfaults otherwise it means that you are not giving delete a valid pointer (it has not been allocated with new or the memory has been already deleted).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by anon View Post
    If the pointer is 0, then delete does nothing at all. You don't delete pointers, you delete what the pointer is pointing at.

    If it segfaults otherwise it means that you are not giving delete a valid pointer (it has not been allocated with new or the memory has been already deleted).
    Ok wait. I think I have been using delete entirely wrong then in my entire program...

    Let's say you do:

    Code:
    Object* myPtr1 = new Object();
    
    Object* myPtr2 = myPtr1;
    
    delete myPtr2;
    What does the delete do?

    A1) myPtr2 wasn't created using "new", so it will give a seg fault???

    A2) If so, what is the proper way to deallocate myPtr2?

    -----

    Another question:

    If I do

    Code:
    Object* myPtr1 = new Object();
    
    myPtr1 = 0;
    
    delete myPtr1; //Useless?
    B1) The delete line does nothing?

    B2) Would this cause a memory leak because the object is still in memory somewhere?




    (Sorry for all the edits, just trying to make it more readable hehe)
    Last edited by Paul22000; 07-24-2008 at 09:41 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course myPtr2 was created using new -- it's right there on the line above! The point being that myPtr2 and myPtr1 have the same value, so deleting one is the same as deleting the other. Assigning myPtr2 to be myPtr1 does not create another copy of your Object.

    Edit: Or perhaps I should say that the Object that myPtr2 is pointing at was created using new.

    As to your second question, you have that backwards -- you need to delete myPtr1 first, then set myPtr1 = 0 (so that you can check there's nothing there).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Paul22000 View Post
    Ok wait. I think I have been using delete entirely wrong then in my entire program...

    Let's say you do:

    Code:
    Object* myPtr1 = new Object();
    
    Object* myPtr2 = myPtr1;
    
    delete myPtr2;
    What does the delete do?

    myPtr2 wasn't created using "new", so it will give a seg fault?
    It will be fine - just don't try to delete myPtr1 as well, because then you do "double delete", since myPtr1 points to the same piece of memory.
    -----

    Another question:

    If I do "myPtr1 = 0; delete myPtr1"

    A) The delete does nothing? And B) it's a memory leak because the object is still in memory somewhere?
    Delete itself does nothing. Whether it means that you leak memory or not depends on what myPtr1 was before it was set to 0. If it pointed to memory that came from new, then you would indeed be leaking that memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> B2) it's a memory leak because the object is still in memory somewhere?
    If you add that code to the previous code, then there is no memory leak (but the delete still does nothing):
    Code:
    Object* myPtr1 = new Object();
    
    Object* myPtr2 = myPtr1;
    
    delete myPtr2;
    
    myPtr1 = 0; // good idea since previous memory pointed to is now invalid
    
    delete myPtr1; // does not leak memory, but does nothing and should be removed

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Let's put it this way...
    When you use new, you are creating a new house out there in the wide world, somewhere.
    New then returns the address where that house is located.
    If you assign ptr1 to ptr2, then ptr2 also holds the address where the house is located.
    When you call delete, the bulldozer drives to the address in the pointer you give it and demolishes the house.
    So if you call delete on ptr1, then what happens to ptr2? Easy. Ptr2 just holds the address of the (now) demolished house! So ptr2 (as well as ptr1) is now invalid.
    If you call delete on ptr2 after ptr1 now, the bulldozer will try to demolish an already demolished house (which, in the computer world, is a big no-no).
    If you set the pointer to 0 before calling delete, the bulldozer can't find the house anymore. Its address is lost and you get a memory leak.

    The concept is that pointers contains the address and no the house (object) itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    If you call delete on ptr2 after ptr1 now, the bulldozer will try to demolish an already demolished house (which, in the computer world, is a big no-no).
    To continue your analogy:

    Worse is that another 'new' may have built a new house at that address, and now deleting ptr2 may demolish another object's house creating a very hard to diagnose/debug problem.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by Elysia View Post
    Let 's put it this way...
    When you use new, you are creating a new house out there in the wide world, somewhere.
    New then returns the address where that house is located.
    If you assign ptr1 to ptr2, then ptr2 also holds the address where the house is located.
    When you call delete, the bulldozer drives to the address in the pointer you give it and demolishes the house.
    So if you call delete on ptr1, then what happens to ptr2? Easy. Ptr2 just holds the address of the (now) demolished house! So ptr2 (as well as ptr1) is now invalid.
    If you call delete on ptr2 after ptr1 now, the bulldozer will try to demolish an already demolished house (which, in the computer world, is a big no-no).
    If you set the pointer to 0 before calling delete, the bulldozer can't find the house anymore. Its address is lost and you get a memory leak.

    The concept is that pointers contains the address and no the house (object) itself.
    Haha! Love the analogy!

    Is it the same through function calls? Say you pass the address to a new function. And in that function, the pointer is deleted. When the program returns, is the house in the calling function also demolished?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes. There is only one house. If delete is called on that address, then the house is destroyed. It doesn't matter where the person/code is that makes the call to delete.

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Agreed. Good analogy.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by Daved View Post
    Yes. There is only one house. If delete is called on that address, then the house is destroyed. It doesn't matter where the person/code is that makes the call to delete.
    Ok, so my program has a heap data structure.

    I have a function that does some sorting of the nodes.

    I have like Node* currentNode = Root; // Root given as function argument
    And I do some operations with that.

    What is the proper way to return the memory from those pointers I'm using to sort things? I shouldn't delte them, because that would delete the actual nodes, right? Do I just not do anything with them at the end of the sorting function?

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Do I just not do anything with them at the end of the sorting function?
    Correct.

    Just declaring a pointer, and setting it to point at memory allocated for another variable, does not use any memory. In your case, you don't have to free or delete currentNode at all.

    Think of it this way. Every new should have one matching delete. Thus, currentNode does not need to be deleted because it was never new'ed.

    This works just fine, for example:
    Code:
    node *root = new node;
    function(root);
    delete node;
    
    void function(node *root) {
        node *some = root;
        // ...
        // some is not deleted or freed
    }
    [edit] In the grand tradition of house demolishers: your code is doing the equivalent of making a photocopy of the bulldozers' contract. It doesn't warrant another bulldozer; it's just duplicating the same information. [/edit]
    Last edited by dwks; 07-24-2008 at 11:22 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-28-2008, 02:46 AM
  2. deleting pointers in the destructor
    By jsrig88 in forum C++ Programming
    Replies: 10
    Last Post: 01-03-2008, 01:01 AM
  3. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 11:16 AM
  4. Help with deleting array of pointers
    By registering in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2003, 11:47 AM
  5. deleting pointers....
    By DirX in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2003, 01:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21