#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    4
    Rep Power
    0

    Freeing a NULL pointer?


    If we want to remove a pointer which points to NULL, how would we go about doing that? This is for C anyway. Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12
    i don't know much about c but i believe you could use the malloc and free commands. Have you looked into that?

    check this out.http://www.iota-six.co.uk/c/40_memor.htm
    Last edited by jonnyfive; September 22nd, 2003 at 05:35 AM.
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    If the pointer is NULL, there is no memory associated with it (whether it was properly free'ed or not). I believe it is always an error to free/delete a NULL pointer. Just check to see if it is NULL and if so, skip the free.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    You cannot 'remove a pointer'. You can free memory that a pointer references if that memory was dynamically allocated. If the pointer has the value NULL (or zero), then it does not point to any allocated memory, so free'ing make no sense, and will cause an error. For safety any pointers you use should be initialised to NULL or zero, and then any free'ing you do should test the pointer first:
    Code:
    if( ptr != 0 )
    {
        free( ptr ) ;
        ptr = 0 ;        // ensure cannot free'd twice
    }
    Clifford
  8. #5
  9. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    As far as I know, calling free() for a pointer that is NULL does not cause any error. Let me check... Take a look at the MSDN page for free():

    "void free( void *memblock );

    ...If memblock is NULL, the pointer is ignored and free immediately returns....
    "
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally posted by Jason Doucette
    As far as I know, calling free() for a pointer that is NULL does not cause any error. Let me check... Take a look at the MSDN page for free():

    "void free( void *memblock );

    ...If memblock is NULL, the pointer is ignored and free immediately returns....
    "
    Which only begs the question that if it works (and it does I tried it at your suggestion), why is he asking the question?

    I know of systems where this does not work (embedded systems admittedly), so I still recommend this approach.

    Clifford.
  12. #7
  13. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    Not all memory functions are that kind

    ie

    GlobalFree()

    "If the hgblMem parameter is NULL, GlobalFree fails and the system generates an access violation exception."
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    What occurs when you free a NULL pointer is dependent upon you libc implementation. Microsoft likes to protect your from your own idiocy, it would seem. Try this with pretty much any other libc implementation and you'll get into trouble. It's good for a segfault on most UNIX systems.

    If you're asking this question at all, expect to see lots and lots of segfaults for a while. You should know what your pointers refer to, and not be freeing them without knowing what you're freeing. The suggestion of zeroing any pointer that has been freed is a good one. It will save you a lot of grief.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  16. #9
  17. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by ClayDowling
    What occurs when you free a NULL pointer is dependent upon you libc implementation. Microsoft likes to protect your from your own idiocy, it would seem. Try this with pretty much any other libc implementation and you'll get into trouble. It's good for a segfault on most UNIX systems.
    Are you referring to the free() function? Since free() is part of the C language, I would assume that MS's implementation follows that of the standard. Usually when there is something specific to Microsoft, it is clearly stated as MS specific in the text. For example, the C++ keywords page has a section enclosed in Microsoft Specific..END Microsoft Specific tags.

    Originally posted by ClayDowling
    If you're asking this question at all, expect to see lots and lots of segfaults for a while. You should know what your pointers refer to, and not be freeing them without knowing what you're freeing. The suggestion of zeroing any pointer that has been freed is a good one. It will save you a lot of grief.
    Agreed.
  18. #10
  19. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally posted by Jason Doucette
    [B]...free() is part of the C language...
    Pedantic point admittedly, but it is part of the ANSI Standard Library for C, not part of the language itself.

    Clifford
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    Okay, a review of OpenBSD source code shows that indeed it bails out when you try to free a NULL pointer. Although this is the most solid code of all the free UNICES, it's probably safe to assume that other libc implementations follow the same procedure. So I'm off to find me a recipe for crow.

    The point remains, however, that it's a good idea to know what your memory refers to before going off to free it.

IMN logo majestic logo threadwatch logo seochat tools logo