Thread: Managing function-scope pointer variable - delete?

  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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,149
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 10:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM