free function

This is a discussion on free function within the C Programming forums, part of the General Programming Boards category; does a content of a pointer is zero after freeing that pointer???...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    46

    free function

    does a content of a pointer is zero after freeing that pointer???

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vapanchamukhi View Post
    does a content of a pointer is zero after freeing that pointer???
    No, not in and of itself. It is customary to set the pointer to NULL (zero) when freeing it, but there is no automatic way that this is done. I have occassionally suggested a function like this:
    Code:
    void freeAndNull(void **p)
    {
        if(p)    // It is possible that someone by mistake is passing NULL as the parameter to freeAndNull... 
        {
           free(*p);
           *p = NULL;
        }
    }
    
    // And an example call:
       char * p = malloc(...);
       
        ...
        freeAndNull(&p);
    ...
    --
    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.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you ever get to a point where you are writing your own memory manager you may find that there are many disadvantages to trying to zero out every block of deallocated memory. It should only be an issue when dealing with security. Other than that, it is a good way to slow down your memory management functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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