Managing function-scope pointer variable - delete?

This is a discussion on Managing function-scope pointer variable - delete? within the C++ Programming forums, part of the General Programming Boards category; If I have a function that has a variable declared inside it (which is a pointer) that can be set ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Managing function-scope pointer variable - delete?

    If I have a function that has a variable declared inside it (which is a pointer) that can be set equal to a pointer returned from another function, do i need to delete it before returning from the function?

    Code:
    void test()
    {
         Test* t = obj.getTest();
         ...elided...
         //do this? (assuming others need to use what's in get() )
         delete t;
         t = NULL;
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by 6tr6tr View Post
    If I have a function that has a variable declared inside it (which is a pointer) that can be set equal to a pointer returned from another function, do i need to delete it before returning from the function?
    That depends on the semantics of getTest(), namely, whether it transfers ownership. If it is giving you a pointer to data you don't "own," you certain should NOT delete it.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    Depends on how the function is implemented, but generally no. It is common practice to have functions responsible for any data they allocate. In this case, the returned pointer is probably stored in obj, and will be deleted when that object is destroyed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This
    Code:
         t = NULL;
    serves absolutely no purpose - the variable t will no longer exist after you return from the function (which is the very next thing that happens), so there's absolutely no meaning to setting the variable to NULL or any other value at this point.

    --
    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.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    But then again, it likely won't hurt because the compiler might just optimize it away. Even if it doesn't, it hardly takes CPU time, so it's not really harmful.
    The best solution would, of course, be to use a smart pointer. Then there is probably no need to set NULL in the first place.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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