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

 node *bucket[65530]={NULL}; 
 unsigned int count=0;

bool load(const char* dictionary)
{

 FILE* inptr = fopen(dictionary, "r"); //Opens file
 char check[LENGTH+1];
 do 
{ 
  node *new= malloc(sizeof(node));  //Create node
  fscanf(inptr, "%s", new->word);   //Get word
  int where=hash(new->word);        //Get hash
  if(&bucket[where]==NULL)         //if Current spot is null
      {    
      bucket[where]=new;           //make head point to node
      new->next=NULL;
      count++;                      //increment count
      } 
  else if(&bucket[where]!=NULL)     //if not NULL
   {
       new->next = bucket[where];    //make next point to wherever bucket does
       bucket[where]=new;           //make bucket point to node
       count++;
   }    
}while(fscanf(inptr, "%s", check)==1);
 return true;
}

This segfaults right after the the else if statement. Valgrind gives me :

Invalid read of size 4

==26992==  Address 0x8025184 is not stack'd, malloc'd or (recently) free'd

I'm so damn close to just giving up at this point. I think something about my pointers is not correct.

Any pointers guys? Help is much appreciated.

0

In looking at your code, I see two fscanf() lines. The second one reads a word into check, but it looks like you're not doing anything with the word loaded into check. That means that you're throwing away half of the dictionary. You do understand that the one in the while clause isn't simply a test, but an actual read, right?

The structure of these reads also leads me to believe that you could hit EOF and still do a read with the first fscanf() line, thus processing a bad read in the code.

Have you determined where you are in the input file when the seg fault occurs? I'm betting that you've process the whole file when it happens.

Looks like time to rethink the logic - maybe figure out how to do it with only one fread and give more thought to EOF processing.

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

1
  • Thanks a ton! I thought that the test would only test and not move the pointer (brain fart). – Curunir The Colorful Nov 18 '15 at 15:09

You must log in to answer this question.

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