【问题标题】:I have a segmentation fault and am unsure about what is wrong with my code我有分段错误,不确定我的代码有什么问题
【发布时间】:2018-01-30 17:08:16
【问题描述】:

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

任何可能指向我正确方向的资源也将不胜感激。

 /**
 * 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;

}

【问题讨论】:

  • 您遇到了什么问题? load 功能不起作用吗?它目前没有做的事情是什么意思?根据提供的有限信息,它看起来应该做你想做的事。
  • 很抱歉最初的措辞不正确。基本上,加载方法必须从字典文件中获取信息,然后允许程序的其余部分使用它。我也遇到了分段错误,我不确定它应该在哪里。我想知道使用 [i] 遍历节点是否也会达到我想要的结论
  • 在调试器(即 gdb)中运行您的代码。它会告诉你分段错误发生在哪里。这是您需要添加到问题中的重要信息
  • 当我在 cloud9 上的 cs50 调试器中运行它时,它告诉我我的问题从第 70 行开始(while 循环的开始)。非常感谢您的宝贵时间。
  • 另外,我的for循环写得好吗?我一直偏执,这是我的另一个错误。对不起,这是我第一次被困在一个程序上,我有点疯了

标签: c list pointers nodes cs50


【解决方案1】:

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

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

    //scan for word
    while(fscanf(dictionaryTextFile, "%s", word) != EOF)

你有两个问题。首先,您没有检查对fopen 的调用是否有效。您应该始终检查返回的值是否不是NULL

其次,崩溃的原因是 word 仍然是 NULL - 你没有分配任何空间来保存字符串。您不妨将其声明为与在 node 中声明的相同,因此请替换

    char *word = NULL;

    char word[LENGTH+1];

谈到node 并且为了避免您以后再次崩溃,您应该始终确保初始化结构的所有属性。在这种情况下,new_node-&gt;next 应该设置为NULL,否则您稍后会在for 循环中检查它(顺便说一句,看起来不错),它可能看起来指向一个节点,但它指向某个节点内存中的随机位置,代码会崩溃。

【讨论】:

  • 我重写并解决了,感谢关心的朋友:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2014-04-11
相关资源
最近更新 更多