#Need Help with free()

26 messages · Page 1 of 1 (latest)

river dust
#

I have a small project, and it's to make a linked-list. I need to support deletion of the whole linked list and reinitialization of a new list.

So, I would have a deleteList(&headList) and headList is a pointer to the malloc where the struct is saved. Inside that function, I traverse the list and freeing the struct element one by one. Then, I simply return. Back in main, I then do headList = NULL; to ensure no dangling pointer. By this point, the program should have acted as if there has never been any entry. I would then create another struct by doing malloc() and assigning that to headList, a pointer that was just pointing to null. However, inserting data after doing this deletion would cause weird things like prevWord pointer pointing to own element making infinite loop. This never happens anywhere except if I do deletion. Basically, I can insert more things into linked list, everything would be fine. But, once I do deletion and initiate a new one, somehow it's giving error. Any direction?

quasi cedarBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hard quarry
#

Try debugging.

river dust
#
// User input stream is limited to 1000 characters
    char userInput[1024] = "";

    // Prompt the user to enter a sentence
    userInputPrompt(&userInput, sizeof(userInput));

    // Initialize wordList struct
    struct wordList* element = (struct wordList*) malloc(sizeof(struct wordList));
    
    // Create and populate linked-list based on user input
    struct wordList* headList = populateWordList(&userInput, element);

The menu to delete:

case '5':
                // Delete current list
                printf("Trying to delete list starting at %s containing %s..\n", &headList, headList);
                deleteList(&headList);
                printf("Deletion successful..\n");
                printf("Setting %s to NULL..\n", headList);
                headList = NULL;
                printf("Checking if %s equal to NULL now..\n", headList);
                headList = (struct wordList*) malloc(sizeof(struct wordList));
                headList->prevWord = NULL;
                headList->nextWord = NULL;
                printf("Setting %s to %s..\n", &headList, headList);
                insertAfterDeletion(&headList);
                printf("%s contains %s now..\n", &headList, headList);
                break;```
river dust
# hard quarry Try debugging.

I'm on replit, not sure where I can do that. I tried googling C debugging, but I am not sure how to use debugging just yet. I've been at this code since yesterday wasting lots of hours. No luck so far. I genuinely can't spot any other mistake I could make. Made sure the pointers are freed and then nulled before initializing a new struct

hard quarry
#

Best to install a C development environment on your own computer.

#

But pretty sure repl.it also has a debugger.

river dust
#

So, after deleteList() call, and after memory allocating new place with the size of the struct, headList is all resetted back to all null, ready to be used

hard quarry
#

Then?

river dust
#

I noticed in the debugging that, even though I initialize new malloc, in the debugging, the variable contains some random garbage value

#

So I now add lines of code to reset everything (word char array to all '\0' and next pointer and prev pointer to all null every time I make malloc

hard quarry
#

malloc doesn't initialize the memory.

river dust
#

It allocates a space in system memory according to specified size right?

hard quarry
#

Yes, but the memory is uninitialized.

river dust
#

And if that address was previously used by other programs, and those programs were closed, now those are garbage values that are free to be overwritten right?

hard quarry
#

That's not relevant.

#

The memory is uninitialized, and you must write something before reading.

#

If you want initialization (to zeroes), there is calloc.

river dust
#

Hm, weird, so I think I fixed it?

#

It was mostly malloc, but didn't initialize to zeros, so it has garbage value

#

and I think when I did a check to see if prev pointer != NULL, since there's technically garbage value, it's considered loop

#

Will still test more though just to be sure, thanks for help

quasi cedarBOT
#

@river dust Has your question been resolved? If so, type !solved :)

river dust
#

!solved