1

Using the debugger, the linked list seems to be successfully created inside the function, but it doesn't get updated "outside" in main. I don't know why it isn't updating since I'm using addresses and dynamic memory allocation, which if I'm not mistaken, doesn't get "cleared" once the function is exited.

int populate(node* list)
{
    node* temp = NULL;

    while(1)
    {
        printf("insert word: ");
        char* word = getstring();
        if(strcmp(word, "stop") == 0)
        {
            break;
        }
        
        //create a node
        node* n = malloc(sizeof(node));
        if(n == NULL)
        {
            return 1;
        }
        
        //put stuff in node
        n->word = word;
        n->next = NULL;
        
        if (list == NULL) //first case only
        {
            list = n;
            temp = n;
        }
        else
        {
            //set previous next to current node
            temp->next = n;
            
            //set pointer to current node
            temp = temp->next;
        }
        
    }
}

int main()
{
    node* list = NULL;
    
    while(1)
    {
        printf("insert command: ");
        char* word = getstring();
        
        if (strcmp(word, "stop") == 0)
        {
            break;
        }
        else if (strcmp(word, "add") == 0)
        {
            populate(list);
        }
        else if (strcmp(word, "read") == 0)
        {
            readList(list);
        }
    }
}

Also, after my code runs, is the memory I've allocated automatically freed? Or am I gobbling up small chunks of my computers memory every time I test my program. (I'm using Xcode)

baltasaurus
  • 11
  • 1
  • 3
  • 3
    in *main* the var *list* is never updated, *populate* has to return the new list head or to get a `list**` to update it. Also *populate* has to return something in any case while not *void* – bruno Jul 03 '20 at 13:12
  • Modern OS will free allocated memories after your code runs. [c - What REALLY happens when you don't free after malloc? - Stack Overflow](https://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc) – MikeCAT Jul 03 '20 at 13:14
  • Consider using a pointer to a pointer to solve your problem – Dean P Jul 03 '20 at 13:16
  • You didn't post the full code of `populate` - at the very least, the `return` statement and the closing brace are missing. – Hulk Jul 03 '20 at 13:19
  • Does this answer your question? [Updating pointers in a function](https://stackoverflow.com/questions/29658614/updating-pointers-in-a-function) – Hulk Jul 03 '20 at 13:20

1 Answers1

1

You need to pass the pointer node* list as a double pointer (pointer to pointer) instead of a pointer:

int populate(node** list)
{

This is because C language has value semantics. Everything is passed by value. So when you pass the list to populate(), you create a copy of the original pointer. They are both pointing to the same memory, but changes to one of the pointer will not be reflected in the other. This is why your list never gets updated.

Everything else will remain mostly the same. When calling the populate function you need to pass the address of the list:

populate(&list);

And in the populate() function, every occurrence of list will become *list since you need to de-reference it to get the original pointer.

Waqar
  • 8,558
  • 4
  • 35
  • 43