Memory management woes

This is a discussion on Memory management woes within the C Programming forums, part of the General Programming Boards category; I'm trying to make a linked list, but my head seems to be staying eternally null. I defined a node ...

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

    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
    Posts
    23,030
    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
    122
    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
    Posts
    23,030
    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, 12:41 PM
  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, 12:25 PM
  5. Replies: 4
    Last Post: 10-11-2002, 07:58 AM

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