0

I have been trying to debug this for over 2 days now. Can't seem to locate the reason why there's a segmentation fault. Running this returns a bunch of misspelled words plus an error message -

speller: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Aborted (core dumped)

Any help would be deeply appreciated guys. Can't seem to make much sense of the error. Here's the load function

   bool load(const char* dictionary)
   {   

   //Open Dictionary with a read only file
   FILE* file = fopen(dictionary, "r");
   if(file == NULL)
    {
      printf("Unable to open Dictionary file \n");
      return false;
    }

    char wd[LENGTH+1];

  //iterate through every word
  while(!feof(file))
 {  
    node* new_word = malloc(sizeof(node));
    fscanf(file,"%s",wd);
    count++;

    strcpy(new_word->word, wd);
    new_word->next = NULL;

   //hash function - Getting the index
   int index = hash_fun(new_word->word);


   if (index >0)
    {   
       if (hashtable[index] == NULL)
         {
            hashtable[index] = new_word;

         }
       else if (hashtable[index] != NULL)
         {  
            node* ptr = hashtable[index]; //initialising the temp pointer
            while(ptr->next != NULL) //taking the pointer to the end of the linked list
                {
                 ptr = ptr->next;
                }
            ptr->next = new_word;
         }
     }
 }

  if(feof(file))
   {
     return true;         
     fclose(file);
   }  

  return false;
  fclose(file);
}

and here's the check function along with the global variables and hash_function

    int count = 0;
    typedef struct node
    {
      char word[LENGTH + 1];
      struct node* next;
    }node;   

    node* hashtable[26];

    int hash_fun(char* key)
    {
     int n = tolower(key[0])-'a';
    return n % 26;
      }

   /**
   * Returns true if word is in dictionary else false.
   */
   bool check(const char* word)
   {   
    char* string = malloc(sizeof(word));
    strcpy(string,word);
    int x = strlen(word);

    for(int i = 0;i<x; i++)
    {
     string[i] = tolower(string[i]);
    } 

    int index = hash_fun(string);

    node* crawler = hashtable[index];

    while(crawler != NULL)
    { 
     if (strcmp(crawler->word,string) == 0)
      {
       return true;
      }
     crawler = crawler->next;
     }
   return false;
  }
5
  • feof returns non-zero after the end-of-file has been reached not whether the next thing to read is the end-of-file.
    – kzidane
    Commented Nov 5, 2015 at 11:42
  • Hi Kareem..thank you for having a look. I did try implementing the code without the feof but the same error persists so i reverted back to this since using feof makes it simpler. I agree with what you said about feof though i don't see it being the issue here for the error. Thank you though! Commented Nov 5, 2015 at 18:49
  • Have you run gdb to try and locate where the problem is? And, have you implemented the unload function at all? The "aborted (core dumped)" is something I've mostly seen when trying to free an already freed pointer. The one thing that strikes me is your line in load: if (index >0) The way you did your hash function, there's no reason for that. Your array will have an index 0. But i don't think that should cause the issue. You need to see whether or not check finishes running. And the easiest way is using gdb
    – Irene
    Commented Nov 6, 2015 at 3:45
  • Hi @Irene...thank you for taking out the time. Commented Nov 6, 2015 at 10:09
  • I tried gdb and sorted out a few minor errors though this error still shows up on executing with austinpowers.txt file. I tried using a smaller text file as an argument and it worked seamlessly on that. Also gave me a green go ahead on the check50. Any other clues? Commented Nov 6, 2015 at 10:29

1 Answer 1

0

I think you need to free allocated memory after

node* new_word = malloc(sizeof(node));
char* string = malloc(sizeof(word));

like this

while(crawler != NULL)
    { 
     if (strcmp(crawler->word,string) == 0)
      {
       free(string);
       return true;
      }
     crawler = crawler->next;
     }
   free(string);
   return false;

It's help me when i get same error.

You must log in to answer this question.

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