简体   繁体   中英

CS50 Speller.c Issue

I have some issues with the CS50 speller task in problem set 5. For those who are not familiar with this problem, basically my program works as a spell checker (you insert a dictionary and a text file and the program returns all misspelled words). Our task was to write five functions (check, hash, load, size, unload). The check function checks if a word is in set dictionary. Hash basically returns a numerical index of the hash table I used to organise the words in based on the input of a word from the text. The load function basically loads all the words from the dictionary file into the hash table. The size function returns the size of the dictionary and finally the unload functions frees all memory allocated to my program. However, when I test my program with check50 there is apparently a fault with the freeing of memory. However, I don't know where this issue could be?

Thanks in advance!

 // Implements a dictionary's functionality

 #include <stdbool.h>
 #include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
 #include <stdlib.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 = 26;

 // Hash table
 node *table[N];

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

}

// Hashes word to a number
unsigned int hash(const char *word)
{
char s = word[0];

if(word[0] < 97)
{
    s = tolower(word[0]);
}
int index = s - 97;

return index;

}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// TODO
FILE *file = fopen(dictionary, "r");

if (file == NULL)
{
    printf("Could not open dictionary.\n");
    return false;
}

char theword[45];

while (fscanf(file, "%s", theword) != EOF)
{
    node *n = malloc(sizeof(node));

    if (n == NULL)
    {
        return false;
    }

    int wordlength = strlen(theword);

    int x = hash(theword);

    char tempword[45];

    if (table[x] == NULL)
    {
        for (int j = 0; j < wordlength; j++)
        {
            n->word[j] = theword[j];
        }

        n->next = NULL;

        table[x] = n;
    }

    for (node *temp = table[x]; temp != NULL; temp = temp->next)
    {
        node *temp2 = temp->next;

        if (temp2 == NULL)
        {
            for (int k = 0; k < strlen(theword); k++)
            {
                n->word[k] = theword[k];
            }
            temp->next = n;

            n->next = NULL;
        }
    }
}
return true;
}

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

for (int i = 0; i < N; i++)
{
    for (node *temp = table[i]; temp != NULL; temp = temp->next)
    {
        count++;
    }
}
return count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
    node *temp = table[i];

    while (temp != NULL)
    {
        node *temp2 = temp->next;
        free(temp);
        temp = temp2;
    }
}
return true;
}

The memory leak is caused by not closing a FILE pointer, but your code has another issue that writes to the outside of a memory space. The following is the error message. Here is the [link] to debug and reproduce this error.

  Memory access warning: memory spaces are not freed; continue execution.
  # 1 memory space is allocated at
  #    file:/musl-1.1.10/src/stdio/__fdopen.c::20, 10
  # total 1164 bytes
  #

  Memory access error: writing to the outside of a memory space; abort execution.
  # Writing 1 bytes to 0xffefc609 will clobber other memory-spaces.
  #
  # The memory-space-to-be-written (start:0xffefc5dc, size:45 bytes) is bound to 'theword' at
  #    file:/dictionary.c::57, 8
  #
  #  0xffefc5dc               0xffefc608
  #  +------------------------------+
  #  |the memory-space-to-be-written|......
  #  +------------------------------+
  #                                  ^~~~~~~~~~
  #        the write starts at 0xffefc609 that is right after the memory-space end.
  #
  # Stack trace (most recent call first) of the write.
  # [0]  file:/musl-1.1.10/src/stdio/vfscanf.c::271, 12
  # [1]  file:/musl-1.1.10/src/stdio/fscanf.c::28, 8
  # [2]  file:/dictionary.c::59, 10
  # [3]  file:/speller.c::40, 19
  # [4]  [libc-start-main]
Segmentation fault

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