Thread: stdlib free puts my tree's node into "to_free_later" list, so I get a segfault.

  1. #1
    Registered User
    Join Date
    Jun 2022
    Posts
    2

    Post stdlib free puts my tree's node into "to_free_later" list, so I get a segfault.

    nodes_count function is called any time I "free" mallocated nodes of the tree and it accesses "freed" memory(it's not NULL) when I do a NULL check:

    Code:
    #define CHILDREN_COUNT aLot
    
    typedef struct node {
        node_info value;
        struct node * children[CHILDREN_COUNT];
    } node;
    
    
    
    // folowing functions are recursive
    
    add_node(node * node_, ...);
    
    free_nodes(node * node_, ...); // follows to the end of branch and starts to call free(node *), but it doesn't actually free memory
    
    nodes_count(node * node_, ...) {
        ...
        if(*(node_->children + index) == NULL) {
            return;
        }
        ...*(node_->children + index)... // accesses "freed" node_
        ...
    }
    
    
    
    int main() {
        ...
        node tree[CHILDREN_COUNT];
        add_node(tree + index, ...);
        free_nodes(tree, ...);
        nodes_count(tree, ...); // segfaults by accessing freed memory
        ...
    }
    count is needed.

    What kind of check should I perform or what is a way around this ?

    Thanks.
    Last edited by vodkapivo123lol; 06-22-2022 at 11:18 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    If I understand what you are asking, maybe just set the pointer to NULL yourself after freeing it. That's pretty common practice.
    Code:
    free(node);
    node = NULL;
    BTW, I've never heard the term "mallocated" before! I kind of like it.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jun 2022
    Posts
    2
    thanks!

  4. #4
    Registered User
    Join Date
    Jan 2022
    Posts
    6
    actually I think that's an incorrect solution you need to set the child to null

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    @natty, I was using the term "node" to mean whatever you freed. The point is that whatever you actually free needs to be manually set to NULL so that you can subsequently tell that it was freed. free() itself does not set it to NULL (as the OP was perhaps expecting).

    You could define a function-like macro to help with this, something like:
    Code:
    #define FREE(p) do{ free(p); p = NULL; }while(0)
    I should have explicitly mentioned that there is no "to_free_later" list. When you free the memory, the memory is freed in the sense that it can be reused by subsequent allocations. It's just that your pointer is not automatically set to NULL. You need to do that yourself.
    Last edited by john.c; 06-24-2022 at 10:15 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-07-2018, 02:40 PM
  2. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  3. Use of puts(str) vs printf("%s", str);
    By wonderwall in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 09:41 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread