0

I have this code, the segmentation fault is now something else. A "Error in `./speller': double free or corruption (fasttop): 0x08220170 Aborted (core dumped)" error. Looks like the program doesn't like the free(temp) line! any clues?

bool unload(void)
{    
    int i = 0;
    while (i < HASHTABLE_SIZE)
    {
        if (hashtable[i] == NULL)
        {
            i++;
        }
        else
        {
         node* temp = NULL;
         node* head = NULL;
         head = hashtable[i];
         while(head->next != NULL)
        {
             temp = head;
             head = temp->next;
             free(temp);
        }
        i++;
        }
    }
1

If hashtable[i] == null, then temp1 == null after the assignment. Now, if you try to access temp1->next when temp1 is null, it generates a seg fault.

Not sure if this fixes the unfreed memory entirely.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • Thanks, I changed the code, the segmentation fault is now something else. A "Error in `./speller': double free or corruption (fasttop): 0x08220170 Aborted (core dumped)",any clue? bool unload(void) { int i = 0; while (i < HASHTABLE_SIZE) { if (hashtable[i] == NULL) { i++; } else { node* temp = NULL; node* head = NULL; head = hashtable[i]; while(head->next != NULL) { temp = head; head = temp->next; free(temp); } i++; } }
    – Askar
    Dec 7 '15 at 19:14
  • Can you update the code in the question and mark which line is causing the error?
    – Cliff B
    Dec 7 '15 at 19:15
  • I have edited the thread, thanks for your help :)
    – Askar
    Dec 7 '15 at 19:29
  • Not sure why you're getting the error (I'd need to actually run it), but there's a bug in the logic anyways. Depending on how you fix the bug, the problem may go away or it may not. The problem is that you're not going to free the end of the linked list. Look at your code and think about the situation at the end of the linked list. head will be not null, but head->next will be null. When you get there, the while test is false, so the last element in the linked list will not be freed, but it will be lost because all of the previous nodes in the linked list have been deleted.
    – Cliff B
    Dec 7 '15 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .