Linked Lists and Memory

This is a discussion on Linked Lists and Memory within the C Programming forums, part of the General Programming Boards category; I'm still getting used to C, i'm writing a basic linear linked list. Am I correct in assuming that the ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Linked Lists and Memory

    I'm still getting used to C, i'm writing a basic linear linked list. Am I correct in assuming that the best coarse of action is to create a "free list" routine. I mean, i can't just free the head pointer and forget about the list right? One more thing, If im allocating memory for the node, and I want to store a string in that node, I have to allocate memory for the string too right? THANKS

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I'm still getting used to C, i'm writing a basic linear linked list. Am I correct in assuming that the best coarse of action is to create a "free list" routine.
    YES, that looks good

    If im allocating memory for the node, and I want to store a string in that node, I have to allocate memory for the string too right?
    If your data structure was like this
    Code:
    struct node
    {
         char *str;
         struct node *next;
    };
    Then YES you will have to allocate memory and make sure u free all those memory when u free the node.

    Or if your data structure looked something like this
    Code:
    struct node
    {
       char str[25];
       struct node *next;
    };
    Then u dont have to allocate memory for your string, cos you have already allocated memory for your string when u create a new node.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! -Linked Lists, Memory Allocation, Time Steps, Debugg
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-14-2009, 09:50 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Memory allocation in linked lists
    By Panserbjorn in forum C Programming
    Replies: 6
    Last Post: 10-27-2007, 12:02 AM
  4. Linked list probs
    By mouse163 in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 05:41 PM
  5. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM

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