【问题标题】:Loading a trie data structure in C- pset5 cs50在 C-pset5 cs50 中加载 trie 数据结构
【发布时间】:2017-01-08 04:00:01
【问题描述】:

我无法将数据加载到我的 trie 结构中。我不断收到一个分段错误。这与我的malloc有关吗?有人发现有什么问题吗?

谢谢

/**
 * dictionary.c
 *
 * Computer Science 50
 * Problem Set 5
 *
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "dictionary.h"


#define ASCII_OFFSET 97;

/** Node of the data structure used to store the dictionary key words
 * Data Structures Type is Tries
 * Includes a bool to indicate if the current node is the end of a word
 * A pointer to the nodes child node
 */
typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;
node* rootNode;
node* nextNode;

//Variable to track the size of the dictinoary
int wordCount = 0;

/**
 * Returns true if word is in dictionary else false.
 */

bool check(const char* word)
{
    //Get words length
    int wordLength = strlen(word);

    for(int i = 0; i < wordLength; i++)
    {
        //taking the character we need to check
        int charToCheck = tolower(word[i]) - ASCII_OFFSET;

        //Checking to see if the char exists in the data strucutre, Trie;
        if(nextNode->children[charToCheck] == NULL)
        {
            return false;
        }

        //Advance the next node down the trie
        nextNode = nextNode->children[charToCheck];
    }

    nextNode = rootNode;
    //Return what is_word return
    return nextNode->is_word;
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */

bool load(const char* dictionary)
{
    //Open dict.. file to read 
    FILE* file = fopen(dictionary,"r");

    //Check if the dict.. exsists!
    if(file == NULL)
    {
        return false;
    }

    //Creating the first node in our data strucutre
    rootNode = malloc(sizeof(node));
    nextNode = rootNode;

    //Get character to store
    int character = fgetc(file) - ASCII_OFFSET;

    //Go through the dict... file 
    while(character != EOF)
    {
        //Go through each word in the file
        while(character != '\n')
        {
            //Add into our data structure
            if(nextNode->children[character] == NULL)
            {
                //Create memory inorder to insert the next node
                nextNode->children[character] = malloc(sizeof(node));
                nextNode = nextNode->children[character];
            }else {
                nextNode = nextNode->children[character];
            }

            //advance character to next
            character = fgetc(file) - ASCII_OFFSET;
        }

        //advance character to next word
        character = fgetc(file) - ASCII_OFFSET;

        //Set the last node loaded to is_word to track the end of each word
        nextNode->is_word = true;
        wordCount++;
        nextNode = rootNode;
    }


    fclose(file);
    return true;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */

unsigned int size(void)
{
    return wordCount;
}

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */

bool unload(void)
{
    for(int i = 0; i < 26; i++)
    {
        if(nextNode->children[i] != NULL)
        {
            nextNode = nextNode->children[i];
            unload();
        }
    }

    free(nextNode);
    return true;
}

【问题讨论】:

  • 恭喜,您已将作业粘贴到 Internet。你的问题是什么?
  • 抱歉,我在加载 trie 时遇到问题,一直收到分段错误。
  • 什么是“trie”? (Ssh,不要帮助 OP!)
  • 不需要#define ASCII_OFFSET 97; 中的分号。但是,如果代码在它存在的情况下编译,您实际上是否在使用ASCII_OFFSET? (答案:是的,它至少被使用了两次,但在表达式的末尾,所以额外的分号只是一个空语句。)使用#define ASCII_OFFSET 'a' 会更清楚吗?
  • 您没有正确初始化分配的 trie 结构元素。您需要确保所有这些指针都为空。

标签: c load trie cs50


【解决方案1】:
  1. 在将 char 用作数组中的索引之前,您不会检查它是否在范围内。任何超出 a-z 范围的字符都会导致缓冲区溢出。

  2. 您将字符与已知的字符常量进行比较,您已从中减去 97。

  3. 您需要通过将NULL 分配给所有数组元素并将is_word 设置为false 来初始化从malloc 返回的内存。

只是看了一眼代码,所以我可能错过了其他错误。

【讨论】:

  • 您还应该使用 calloc 而不是 malloc,因为您检查 NULL 但 malloc 没有给您定义的内存值。
  • @maxbit89 你提出了一个有效的观点。我不认为NULL 按照标准必须是0(即使“所有”实现似乎都将它作为0),所以我选择建议明确设置指向NULL 的指针而是。
  • 是的,它没有在标准中定义,但很好用^^。另见:stackoverflow.com/questions/9894013/is-null-always-zero-in-c
猜你喜欢
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多