Thread: Freeing a buffer in a node.

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Freeing a buffer in a node.

    I've used the free function to free an array that was part of a struct. However I got some segfaults when running the program, so I decided to print out the array to see what had happened to it.

    I saw that the buffer had a mix of garbage values and part of the original string.

    Essentially, I had called free like:

    Code:
    free(node->string);
    then I did

    Code:
    printf("%s", node->string);
    got garbage as a result. Isn't free() supposed to null whatever you are meant to be freeing?
    =========================================
    Everytime you segfault, you murder some part of the world

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Isn't free() supposed to null whatever you are meant to be freeing?
    No

    it just markes a reageon as availeble for usage, it does not modifies memory contents, and does not updates your pointer with null value,

    using the freed pointer - is undefined behavior
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's because it's undefined behaviour. It can crash. It can not crash. It is not guaranteed to crash either.
    The rule is: just don't do it.
    Free simply tells the operating system you don't want that piece of memory any more. Then it's up to the OS what it wants to do with it.
    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.

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Hmm, I seemed to have stopped the segfaults, a stray something somewhere.

    But if I free the memory in array in a struct, then I can malloc that memory again right? Without having to create a new node all the time.
    =========================================
    Everytime you segfault, you murder some part of the world

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure you can. The memory is now free and unusable, so you need to malloc again to use a new piece.
    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.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Cheers
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    if you create a temporary variable so that you store a copy of the node in that variable.

    And you then free the node, why does the temporary variable also get freed?
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, not if you make a copy. Since pointers do just contain addresses, if you have two pointers to the same data and if you free one, the other will also be freed.
    But if you make a copy, then they will have two different addresses and thus only one gets freed.
    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.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Ahhhhhh pointers, that's what I did by mistake, I created a pointer instead of making a node and then assigning it. No wonder it got freed too.

    Thanks Elysia. You're a programming master! How the hell do you know every thing?
    =========================================
    Everytime you segfault, you murder some part of the world

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM