2

Hi I'm tackling CS50's . I am trying to implement and I got stuck on check part. I implemented load via hashtables and whole dictionary loads up correctly,when I'm using smaller text files everything is ok but when i load large dictionary im getting segfault.I usually don't ask for help but this time I'm getting weird errors and even valgrind failed to check my code, it just ended with instructions where to report bug.

Global variables I have are words for size function and definition of struct node.

I haven't freed what i malloced because i understood that is a job for unload. Tried even to free allocated memory but problem persisted. I would really appreciate any kind of guidance.I read a lot from stackexchange but i haven't figured out solution yet.Thanks

int words = 0;

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

node* hashtables[26];
int hash_function(const char *key)
{
    int hash = (key[0] - 'a') % ChaNum;
    return hash;
}

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

    for(int i = 0,n = strlen(word);i < n;i++)
    {   
        if(isupper(word[i]))
            lowcop[i] = tolower(word[i]);
        else
            lowcop[i] = word[i];
    }

    int z = hash_function(lowcop);

    node* cursor = malloc(sizeof(node));
    cursor = hashtables[z];

    if(z >= 0)
    {
        while(cursor != NULL)
        {
            if(strcmp(cursor -> word,lowcop) == 0)
                return true;
            cursor = cursor -> next;
        }
    }
    return false;
}

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

    FILE* fpt = fopen(dictionary,"r");

    node* head = NULL;    
    if(fpt == NULL)
    {
        printf("Sorry,we're unable to unable to open your file\n");
    }

    while(!feof(fpt))
    {
        node* new_word = malloc(sizeof(node));
        fscanf(fpt,"%s",new_word -> word);

        int j = hash_function(new_word -> word);

        if(j >= 0)
        {  
            if(hashtables[j] == NULL)
            {
                hashtables[j] = new_word;
                head = hashtables[j];  
                head -> next = NULL;              
                words++;
            }
            else if(hashtables[j] != NULL)
            {
                // printf("%s and %d\n",hashtables[j] -> word,j); 

                new_word -> next = head -> next;
                head -> next = new_word;
                words++;
            }
                // printf("%s and %d\n",hashtables[j] -> word,j); 
        }

    }

    if(feof(fpt))
    {
        return true;
        fclose(fpt);
    }
    return false; 
    fclose(fpt);
}

/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
    //TODO
    return words;
}

/**
* Unloads dictionary from memory.  Returns true if successful else false.
*/
bool unload(void)
{
    // TODO
    return false;
}
1
  • In load method, at the end you try to close a file after return. After return, no more lines of this method will be executed. – Hrvoje T Jul 23 '20 at 5:52
1

I just found out what was the error i think,I replaced char* lowcop with array size of given word and it seems to work.I knew it was something small,I guess i answered my own question.This was not intentional

2
  • 1
    Answering your own question is perfectly acceptable, even encouraged. Now that you've found the answer, could you please accept the answer so that the question is marked as answered and taken out of the unanswered pool? Let's keep up on forum maintenance. ;-) – Cliff B May 10 '15 at 6:24
  • I have the same problem. Could you please explain to me what you mean by I replaced char* lowcop with array size of given word. I tried doing char* buffer [len] instead of char* buffer = malloc(sizeof(len)) and I get error: incompatible pointer types passing 'char *[len]' to parameter of type 'const char *' when passing it to strcmp. I also get error: incompatible integer to pointer conversion assigning to 'char *' from 'int' when passing to tolower. – William K Apr 22 '20 at 18:45
0

I think you also have N set to 1. You should probably change the value of N - it's the 'number of buckets' you want your data to get sorted into.

I read that N should be a prime number, and higher than the number of keys you're expecting to have (but I'm just another student, so I might be wrong).

You must log in to answer this question.

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