Question about free()

This is a discussion on Question about free() within the C Programming forums, part of the General Programming Boards category; When I look at free()'s manpage , it mentions that its behaviour is undefined if I pass it a pointer ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64

    Question about free()

    When I look at free()'s manpage, it mentions that its behaviour is undefined if I pass it a pointer which hasn't been returned by malloc(), calloc() or realloc(), or if the pointer I pass to it had the memory it's pointing to "free()'d" before.

    What if I pass it a pointer which is a copy of a pointer returned by, say, malloc()? For example:

    Code:
    (...)
    
    void free_int_pointer(int* ptr) {
        free(ptr);
    }
    
    int main() {
        int* my_pointer = malloc(sizeof(int));
        free_int_pointer(my_pointer);
        return 0;
    }
    Is this OK to do, even though the pointer that free() receives is a copy of the original "my_pointer"?

    Thank you in advance.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should pass the same value that you received from malloc.

    As in your sample - you pass the exactly the same value (and do it exactly once) - it is ok
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, as vart says, it's the value of the pointer (or put another way, the address you get back from malloc) that is referred to here, not the variable in which it is stored.

    So if you get 0x12345600 back from malloc(), you need to free the same address 0x12345600, but where that is stored is up to you, and if you want to shift it around in 15 different variables before you free it, that's also up to you.

    You also should not free memory that didn't come from malloc in the first place, e.g.
    Code:
    char *ptr = "Some string";
    ...
    free(ptr);
    will most likely crash the system - or cause other undefined behavior.

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

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    You should set the my_pointer to NULL so that you do not have any dangling references anymore once you free().

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by pankaj401 View Post
    You should set the my_pointer to NULL so that you do not have any dangling references anymore once you free().
    Will not work if you have a copy of this pointer somethere else... For example in the calling function.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design question
    By MacGyver in forum C Programming
    Replies: 9
    Last Post: 05-17-2007, 03:36 AM
  2. Simple question about free()
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-16-2006, 12:14 PM
  3. question about free()
    By TalosChen in forum C Programming
    Replies: 5
    Last Post: 05-20-2006, 07:11 PM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 08:52 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

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