0

All checks with check50 are green except the memory error. This is what valgrind shows me:

==3259== 
==3259== HEAP SUMMARY:
==3259==     in use at exit: 8,013,096 bytes in 143,091 blocks
==3259==   total heap usage: 143,096 allocs, 5 frees, 8,023,416 bytes 
allocated
==3259== 
==3259== 8,013,096 bytes in 143,091 blocks are still reachable in loss record 
1 of 1
==3259==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck- 
amd64-linux.so)
==3259==    by 0x401180: load (dictionary.c:82)
==3259==    by 0x400924: main (speller.c:40)
==3259== 
==3259== LEAK SUMMARY:
==3259==    definitely lost: 0 bytes in 0 blocks
==3259==    indirectly lost: 0 bytes in 0 blocks
==3259==      possibly lost: 0 bytes in 0 blocks
==3259==    still reachable: 8,013,096 bytes in 143,091 blocks
==3259==         suppressed: 0 bytes in 0 blocks
==3259== 
==3259== For counts of detected and suppressed errors, rerun with: -v
==3259== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

MY CODE :


// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 27;

int sz=0;


// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int index = hash(word);
    node *ptr = table[index];
    if(ptr != NULL)
    {
        while(ptr!=NULL)
        {
           if(strcasecmp(ptr->word,word) == 0)
           return true;

           ptr = ptr->next;
        }
    }

    return false;
}

// Hashes word to a number

unsigned int hash(const char *word)
{

    unsigned int first_letter = word[0];

    if(first_letter>=65 && first_letter<=90)
    first_letter += 32;

     unsigned int h;
     h = first_letter;

     return h;

}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *f;
    f = fopen(dictionary,"r");
    if(f == NULL)
    {
        fclose(f);
        return false;
    }
    for(int i=0; i<N; i++)
    table[i] = NULL;

    node *n;
    char words[45];
    while(fscanf(f,"%s",words) != EOF)
    {

        n = malloc(sizeof(node));
        if(n == NULL)
        {
          fclose(f);
          return false;
            
        }

        strcpy(n->word, words);
        int p = hash(words);
        n->next = table[p];
        table[p] = n;
        sz++;
    }
    fclose(f);

    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{

    return sz;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
     if(sz == 0)
     return false;

    for(int i=0; i < N; i++)
    {
        node *p = table[i];
         while(p != NULL)
         {
            node *temp = p;
            p = p->next;
            free(temp);

          }
          table[i] = NULL;

     }
    return true;
}
6
  • I tested the code and couldn't reproduce the problem. Unless there's a bug in the unposted code, can't help. Are you sure that you recompiled the latest code before testing it?
    – Cliff B
    Jul 8 '20 at 23:38
  • yes, the code I uploaded has been compiled.
    – Neev Mehta
    Jul 9 '20 at 3:22
  • What does this mean - in use at exit: 8,013,096 bytes in 143,091 blocks
    – Neev Mehta
    Jul 9 '20 at 3:23
  • It means that when the program ended, there were 8MB of data that hadn't been freed but still could. That data was in 143,091 blocks - exactly the same number of blocks as the number of words in the dictionary. Hmmm.....
    – Cliff B
    Jul 9 '20 at 3:36
  • So, could you please edit the question and post the entire code?
    – Cliff B
    Jul 9 '20 at 3:37
1

Interesting that the program actually works without causing a seg fault. I suspected that the problem wasn't in load or unload.

Here's a big hint: What hash values are returned for each word when using the small dictionary?

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

2
  • Yes, I figured it out. Simply changing the size of table to fit the index hash was returning did the trick. Thank you so much for helping me !
    – Neev Mehta
    Jul 9 '20 at 22:40
  • Well, that's like buying a dump truck to bring home some dirt for our garden when you could have rented a small trailer. Increasing the size of the table to match the hash is doing it backwards. Instead the better approach is to make sure that the hash function matches the table size. Hint: %27 is your friend. I'll let you figure out why this is important and how it would work.
    – Cliff B
    Jul 9 '20 at 23:44

You must log in to answer this question.

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