我有一个分段错误,我不确定我的代码有什么问题

时间:2018-01-30 17:08:16

标签: c list pointers nodes cs50

任何指导都将不胜感激。我个人认为问题在于加载方法。此外,每个方法的基本功能都写在注释中。可能是我的分段错误的原因是什么?一切都按预期工作了吗?谢谢你的时间。

任何可能指向我正确方向的资源也会受到赞赏。

 /**
 * Implements a dictionary's functionality.
 */

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

//Defining node:
 typedef struct node
 { //Inner workings of each "element" in the linked lists
    char word[LENGTH + 1]; //the word within the node is +1'd due to the memory after the word containing /0
    struct node *next; //linked list
}node;



  node *alphabetList[27]; //26 buckets that can contain variables of type node(of dynamic size)
//one bucket for each letter of the alphabet
  node *cursor = NULL;
  node *head = NULL;

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    int bucketIndex ;
    //no need to malloc information b/c we are simply pointing to previously established nodes.
    if(word[0] >= 65 && word[0] < 97){
        bucketIndex = word[0] - 65;
    }
    else{
        bucketIndex = word[0] - 97;
    }


    node *head = alphabetList[bucketIndex];

    node *cursor = head;

    while(cursor != NULL)
    {
        cursor = cursor -> next;
        if(strcmp(cursor -> word, word) != 0)
        {
            return true;
        }

    }


    return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
        char *word = NULL;
        int i = 0; //index
        FILE *dictionaryTextFile;
        dictionaryTextFile = fopen(dictionary, "r");

        //scan for word
        while(fscanf(dictionaryTextFile, "%s", word) != EOF)
        {
            //for every word we scan we want to malloc a node to ascertain we have sufficent memory
            node *new_node = malloc(sizeof(node));
            if(new_node == NULL) //error check(if you run out of memory malloc will return null)
            {
                unload();
                return false;
            }
            //error check complete.
            else{
                strcpy(new_node -> word, word);
            }


            //not sure from here on 
            char first_letter = new_node[i].word[0]; //first letter of node word (confused on how to execute this properly)

            first_letter = tolower(first_letter);
            int index = first_letter - 97;

            if(word){

                for(node *ptr = alphabetList[index]; ptr!= NULL; ptr = ptr->next)
                {
                    if(!ptr-> next){
                        ptr->next = new_node;
                    }
                }

            }
            else
            {
                alphabetList[index] = new_node;
            }
            i++;
            }


     return true;

    }





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

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{
    for(int i = 0; i <= 26; i++)
    {

    node *head = alphabetList[i];
    node *cursor = head;

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

    }
return true;

}

1 个答案:

答案 0 :(得分:0)

问题很明显,现在你已经说过代码崩溃了哪一行。考虑这些线......

    char *word = NULL;
    int i = 0; //index
    FILE *dictionaryTextFile;
    dictionaryTextFile = fopen(dictionary, "r");

    //scan for word
    while(fscanf(dictionaryTextFile, "%s", word) != EOF)
你在那里遇到了2个问题。首先,您不会检查对fopen的调用是否有效。您应该始终检查返回的值是否为NULL

其次,崩溃的原因是word仍然是NULL - 你没有分配任何空间来保存字符串。您也可以声明它与在node内声明它相同,所以替换

    char *word = NULL;

    char word[LENGTH+1];

说到node并为了避免以后再次发生崩溃,你应该始终确保初始化结构的所有属性。在这种情况下,new_node->next应设置为NULL,否则您将在for循环(看起来很好BTW)后来检查它,它可能看起来指向一个节点,但它指向内存中的一些随机位置,代码将崩溃。