Node disallocation

This is a discussion on Node disallocation within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> typedef struct _Node Node; struct _Node { int ID; int ParentID; int Date_of_birth; int SpounceID; ...

  1. #1
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142

    Node disallocation

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct _Node Node;
    
    struct _Node
    {
     int ID;
     int ParentID;
     int Date_of_birth;
     int SpounceID;
     int k;
     int Generation;
    
    };
    
    int main(void)
    {
     Node *lala;
     
         lala=(Node *)malloc(sizeof(Node));
    
         lala->ID=10;
         lala->ParentID=12;
         lala->Date_of_birth=1980;
         lala->SpounceID=89;
         lala->k=5;
         lala->Generation=2;
         
         printf("&#37;d %d %d %d %d %d\n",
              lala->ID,
              lala->ParentID,
              lala->Date_of_birth,
              lala->SpounceID,
              lala->k,
              lala->Generation);
    
         free(lala);
         
         printf("%d %d %d %d %d %d\n",
              lala->ID,
              lala->ParentID,
              lala->Date_of_birth,
              lala->SpounceID,
              lala->k,
              lala->Generation);
    
    
     return 0;
    }
    After free, printf still prints all info (ID,ParentID...) as i never disallocate lala node.
    Is there another way to free lala node or i did something wrong?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > as i never disallocate lala node.
    Yes you did, you called free().
    That's it, end of memory, nothing more to see here.

    > After free, printf still prints all info (ID,ParentID...)
    Except your code is broken for even trying it.
    Sometime later, on another machine, you're either looking at garbage or a segfault.

    The memory doesn't physically disappear, only your right to access it. It might lie around for a while in an otherwise unused state, remembering (as memory does) the last thing stored there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Thanks Salem

    So if i set lala pointer to NULL in order (by checks) to avoid segfaults and continue executing code there will be no problem as far as memory and faults concern. Right ?

    ex.
    Code:
         free(lala);
         lala=NULL;
         
         if(lala==NULL)
            printf("Node is empty");
         else
             //code here
    I mind about memory problem such as "not enough". I expected physically memory disallocation.
    If i use free for a char * after, i can't print the string so, i thought that something like segfault i would get if try to print Node info.
    Last edited by ch4; 11-01-2008 at 02:37 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    After freeing the memory, accessing it again is undefined behavior. It might work, it might not.
    So yes, it is good behavior to make the pointer that you freed NULL. Even better is if you make the pointer go out of scope once you free 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.

  5. #5
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    I understood.

    Thank you both !

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, 03: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, 09: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

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