0

I have been stuck on this pset for over a week now. I have already checked other answers to similar questions here but none have been helpful to me. Testing through check50 as well as the debugger I have identified the problem as being with my check and unload functions. My check function strangely doesn't handle a basic word like "The" nor is it spell-checking case-insensitively (even though I specifically used the strcasecmp function). Running my program through valgrind using a small dictionary and text I see that my unload function frees most of the memory but valgrind says that 552 bytes in 1 block is still reachable.

The code for both functions are below, and I have also included the code for my load function, as I have seen others mention that an issue with the load function can impact the others, though I don't think that is true for my case.

// My check function

// Returns true if word is in dictionary else false

bool check(const char *word)
{
  unsigned int index = hash(word);  
    
  node *trav = table[index];      
    
    while(trav != NULL)
    {
        if(strcasecmp(word,trav->word) == 0) 
        {
            return true;
            
        }
        else
        {
            trav = trav->next;  
        }
    }
    
    return false;
}

// My load function

// Loads dictionary into memory, returning true if successful else false

bool load(const char *dictionary)
{
    // Open dictionary file
    
    FILE *ptr = fopen(dictionary, "r");
    if(ptr == NULL)
    {
        printf("Failed to open dictionary file\n");
        return false;
    }
    
    // Read strings from file one at a time
    
    char buffer[LENGTH + 1];
    
    while(fscanf(ptr,"%s", buffer) != EOF)
    {
        // Create a new node
        
        node *n = malloc(sizeof(node));
        if(n == NULL)
        {
            return false;
        }
        
        strcpy(n->word, buffer);
        n->next = NULL;
        
        // Hash word
        
        unsigned int index = hash(n->word);
        
        // Insert node into Hash table 
        
        n->next = table[index];
        table[index] = n;
        
        word_count++;
        
    }
    
    return true;
}

// My unload function

// Unloads dictionary from memory, returning true if successful else false

bool unload(void)
{
    node *trav = NULL;
    node *tmp = NULL;
    
    for(int i=0; i < N; i++)
    {
        trav = table[i];
        
        while(trav != NULL)
        {
            tmp = trav;
            trav = trav->next;
            free(tmp);
            
        }
    }
    return true;
}


I have gone over this code multiple times, reviewing it with the walkthroughs each time, and still can't see what the issue might be. So any suggestions as to why my code isn't working correctly will be helpful.

1 Answer 1

1

The hash function is the root of the case-sensitive problem, otherwise strcasecmp would be sufficient.

The memory leak appears to be because the file pointer to dictionary is never closed.

2
  • Ugh stupid mistake... I overlooked not closing the file pointer.... Can you explain further how you know that there is an issue with my hash function? I didn't include it here, and it is a cited one I found online. Should I include it for your convenience? Commented Oct 1, 2020 at 0:23
  • Occam's Razor (case-sensitive hash is not an uncommon mistake). No reason to include hash code. Commented Oct 1, 2020 at 13:52

You must log in to answer this question.

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