0

Hey guys! I have been working on pset5 for a while and I keep on obtaining a seg fault. I have alreay run valgrind but it gives me no error. I cannot really see where is the problem. Any help will be really appreciated! Thanks in advance

typedef struct node
{
        bool is_word;
        struct node* alpha[27];
}
node;

unsigned int dicSize = 0; 
struct node* root = NULL;


bool check(const char* word)
{
node* next = root;
unsigned int k = 0;
int i = 0;

while(k != '\0')
{
    k = word[i];

    // switch to lowercase
    if(k >= 65 && k <= 90  )
    {
        k = tolower( k);
    }

    if((k <= 122 && k >= 97 ) || (k == '\''))
    {

        if(k == '\'')
            k = 123;

        if(next->alpha[k - 97] == NULL)
        {
            return false;
        }
        else
        {
            next = next->alpha[k - 97];
        }
    }

    i++;
}

return next->is_word;
}

 /**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{


FILE* fmine = fopen(dictionary, "r");
if (fmine == NULL)
{
    return false;
}
node* root = malloc(sizeof(node));

node* next = root;
unsigned int read= 0;

do  
{

    read = fgetc(fmine);
    if(read == '\'')
     read = 123;

        if(next->alpha[read - 97] == NULL)
        {
            next->alpha[read - 97] = malloc(sizeof(node));
        }
        else (next = next->alpha[read - 97]);



    }while (read != EOF && read != '\n');

    if(read == '\n')
    {
        next->is_word = true;
        dicSize++;
        next = root;


    }

    fclose(fmine);

    return true;

   }
0

You actually have multiple problems causing seg faults. In addition to Kareem's answer, the code hits (with a very small dictionary) one in the do/while loop structure:

do{
....    
}while (read != EOF && read != '\n');

This loop will read the dictionary and process it, but it doesn't deal with EOF correctly. It will read past the end of the file, causing the seg fault, before the EOF is detected by your code. You should restructure to check for EOF immediately at or after the read, maybe with an if/break combination, and not wait until the end of the while loop.

There's also a seg fault lurking in the check() function, but I'll let you sort out the load() problems first and try to fix the check() problem on your own. ;-)

If either of these answers solves your problem, please click the appropriate check mark. Let's keep up on forum maintenance. ;-)

1
  • Thanks so much! I have been able to fix the segfault! In both check and load. Now I have an issue with the misspelled words, but I'll try to figure out – Stefano Perrotta Dec 6 '15 at 0:14
0

I have checked your code with valgrind and actually I have many takes on the code, but regardless of anything else and just to answer your question, valgrind says that the line which causes the segfault is

next->is_word = true;

if you tried to see why, you'd know that you are never handling the case where read == '\n' except inside the loop continuation condition. what happens if read == '\n'? well, you end up with a negative index because take 97 from '\n' or 10, that's gonna be -87.

so you're gonna end up with a wrong pointer. trying to allocate memory for that pointer to point at or to access a member called is_word through it may definitely cause a segfault!

You must log in to answer this question.

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