简体   繁体   中英

Program is free of memory errors, valgrind tests failed - CS50 pset5 speller

I'm am getting this error - program is free of memory errors, valgrind tests failed; see log for more information.

I have tried so many things but I keep getting more errors. Here is my code:

// Implements a dictionary's functionality

#include <stdbool.h>
#include <strings.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "dictionary.h"

// 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 = 1;

unsigned int numWords = 0;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    //initialize index for hashed word
    int hashNum = hash(word);
    
    //set cursor point to same address as hashtable bucket
    node *cursor =  table[hashNum];
    
    //while the cursor doesn't show NULL, seach the dictionary for the word
    while(cursor != NULL)
    {
        //the word is found if the strcasecmp returns the value of true
        if (strcasecmp(cursor->word, word) == 0)
        {
            return true;
        }
        //if the word has not been found then the cursor will advance
        cursor = cursor->next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    char letter = toupper(word[0]);
    return ((int) letter) - ((int) 'A');
}

int word_count = 0;

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    //open the dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }
    
    //scan the dictionary word by word
    char str[LENGTH + 1];
    while (fscanf(file, "%s", str) != EOF)
    {
        node *newNode = malloc(sizeof(node));
        //checks if malloc succeeded; if it is not succeeded then return false
        if (newNode == NULL)
        {
            return false;
        }
        
        //initializes and calculates index of the word for insertion into hashtable
        int hashNum = hash(str);
        
        //copy word into node if malloc succeeds
        strncpy(newNode->word, str, sizeof(str));
        
        //initialize to point the head to hashtable of index
        //node *head = hashtable[h];
        
        //insert the new node at the beginning of the lists
        if (table[hashNum] == NULL)
        {
            newNode->next = NULL;
        }
        else
        {
            newNode->next = table[hashNum];
        }
        table[hashNum] = newNode;
        numWords += 1;
        
    }
    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    for (int i = 0; i < N; i++)
    {
        if (table[i] != NULL)
        {
            free(table[i]);
        }
    }
    return true;
}

Here is the log with the error message. It says it's coming from line 76:

running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmpgm2cu85v -- ./speller substring/dict substring/text... 
checking for output "MISSPELLED WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS IN TEXT: 6\n"... 
checking that program exited with status 0... 
checking for valgrind errors... 
112 bytes in 2 blocks are still reachable in loss record 1 of 1: (file: dictionary.c, line: 76) 

Here is line 76:

node *newNode = malloc(sizeof(node));

What am I doing wrong?

The posted code does not compile:

Fix the compile problems first.

gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled.c" -o "untitled.o" 

untitled.c:25:7: error: variably modified ‘table’ at file scope
   25 | node *table[N];
      |       ^~~~~

untitled.c: In function ‘check’:
untitled.c:31:19: warning: implicit declaration of function ‘hash’ [-Wimplicit-function-declaration]
   31 |     int hashNum = hash(word);
      |                   ^~~~

untitled.c: At top level:
untitled.c:51:14: error: conflicting types for ‘hash’
   51 | unsigned int hash(const char *word)
      |              ^~~~

untitled.c:31:19: note: previous implicit declaration of ‘hash’ was here
   31 |     int hashNum = hash(word);
      |                   ^~~~

untitled.c: In function ‘hash’:
untitled.c:54:19: warning: conversion from ‘int’ to ‘char’ may change value [-Wconversion]
   54 |     char letter = toupper(word[0]);
      |                   ^~~~~~~

untitled.c:55:27: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
   55 |     return ((int) letter) - ((int) 'A');
      |            ~~~~~~~~~~~~~~~^~~~~~~~~~~~~

untitled.c: In function ‘load’:
untitled.c:83:23: warning: conversion to ‘int’ from ‘unsigned int’ may change the sign of the result [-Wsign-conversion]
   83 |         int hashNum = hash(str);
      |                       ^~~~

untitled.c: In function ‘unload’:
untitled.c:119:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
  119 |     for (int i = 0; i < N; i++)
      |                       ^

Compilation failed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM