Thread: Memory management woes

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Memory management woes

    I'm trying to make a linked list, but my head seems to be staying eternally null. I defined a node struct and my head
    Code:
    typedef struct node_s
    {
       int id;
       data_t data;
       struct node_s *next;
    } node_t;
    
    node_t *headNode = NULL;
    I created an insert function that should allow for inserting id and data into any list
    Code:
    void insert(int id, data_t data, node_t **head)
    {
       node_t *cur = NULL;
    
       nodeDup(cur, *head);
      //find where to insert
    
      if(cur == NULL)
      {
         *head = (node_t *)malloc(sizeof(node_t));
         //assign values
      }
    }
    
    void nodeDup(node_t *v1, node_t *v2)
    {
      if(v2 == NULL)
        return;
    
      v1 = (node_t *)malloc(sizeof(node_t));
    
      v1->id = v2->id;
      //etc
    }
    
    int main()
    {
    ...
    insert(1, data, &headNode);
    insert(2, data, &headNode);
    ...
    }
    cur is always null, even in successive calls to insert. Shouldn't "*head = (node_t *)malloc(sizeof(node_t));" be causing headNode to no longer be null?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Only the local pointer v1 is assigned a result from malloc. The local pointer cur is not affected.
    Did you forget about pointer to pointers?
    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.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Ah, ok. Thanks. So my dup function was wrong. I changed it to return a pointer and assigned cur to it and it worked. I guess I could have also made v1 a double pointer and that would have worked too, right?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah it would.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Management: Swapping in data
    By cse_smita in forum C Programming
    Replies: 8
    Last Post: 01-17-2010, 01:50 PM
  2. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  3. Valgrind says I suck at memory management :)
    By carrotcake1029 in forum C Programming
    Replies: 6
    Last Post: 02-01-2009, 08:10 PM
  4. Memory Management
    By black_spot1984 in forum C++ Programming
    Replies: 9
    Last Post: 10-08-2008, 11:25 AM
  5. Replies: 4
    Last Post: 10-11-2002, 06:58 AM