Thread: A simple question

  1. #1
    Registered User
    Join Date
    Aug 2006
    Location
    In my wonderful fortress of doom and solitude
    Posts
    2

    A simple question

    Read this piece of code:

    Code:
    /*An empty pointer*/
    struct node *root_node;
    
    root_node = malloc(sizeof(struct node));
    
    free(root_node);
    Does the entire instance of the structure which the pointer points to get freed up or does only the first element in the structure gets freed up. It may seem trivial but I have no way of actually testing this.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It free()s the entire instance of the structure. Just remember, for every malloc() you need to have exactly 1 free(). Always.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Do you mean something more like, if node was defined like

    Code:
    struct node
    {
          struct node * next;
    
          // Other elements
          // Other elements
    }
    And you were actually like

    Code:
    root_node = malloc(sizeof(struct node));
    rootnode->next = malloc(sizeof(struct node));
    rootnode->next->next = malloc(sizeof(struct node));
    rootnode->next->next->next = malloc(sizeof(struct node));
    rootnode->next->next->next->next = NULL;
    Would the whole list be freed? Or do you really mean the first element of the structure.

    First case, no. You would make a routine to do so.
    Second case, no. The whole structure is freed.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    You'd do it in a loop, as you can't know how many you'll have.
    When you create a new node (reserve the space using malloc()), set the next pointer to null until another node is created, then overwrite that null value with a pointer to the newly created node, and so on.

    To release it, scan from the start and follow the chain. When the next value is null, you've reached the end. You'll need to use a temporary intermediate variable.

    What you're after here is a linked list; have a look here for an article about them.
    I think you can put a signature here.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Location
    In my wonderful fortress of doom and solitude
    Posts
    2

    Thx

    Thanks for the help. You've answered my questions. I'm sorry I should have been more specific: I am in fact making a linked list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM