C.malloc.C:没有这样的文件或目录

C.malloc.C:没有这样的文件或目录,c,cs50,C,Cs50,这是我的密码: // Implements a dictionary's functionality #include <stdbool.h> #include <stdio.h> #include <cs50.h> #include <ctype.h> #include <string.h> #include <strings.h> #include <stdlib.h> #include "diction

这是我的密码:

// Implements a dictionary's functionality

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

#include "dictionary.h"

// Maximum length for a word
#define LENGTH 45

//define struct node
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

//hash function
unsigned int hash(const char word[LENGTH + 1])
{
    int x = atoi(&word[0]);
    int y;
    if(isupper(word[0]))
    {
        y = (x + 13) % 26;
    }
    if(islower(word[0]))
    {
        y = (x + 7) % 26;
    }
    if(isalpha(word[0]) == 0)
    {
        return 0;
    }
    return y;
}

//make a hash table of linked lists
node* hash_table[26];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    node *head = hash_table[hash(word)];
    node *cursor = head;
    while (cursor != NULL)
    {
        if (strcasecmp(word, cursor->word) == 0)
        {
            return true;
        }
        cursor = cursor->next;
    }
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char word[LENGTH + 1];
    FILE *dictionary_file = fopen(dictionary, "r");
    if (dictionary_file == NULL)
    {
        unload ();
        return false;
    }

    while (fscanf(dictionary_file, "%s", word) != EOF)
    {
        //allocate memory for node
        node *new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            unload();
            return false;
        }

        //insert node into linked list
        int index = hash(word);
        node *head = hash_table[hash(word)];

        //place word in node
        strcpy(new_node->word, word);

        //connect nodes
        new_node->next = head;
        hash_table[index] = new_node;
        }
    fclose(dictionary_file);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    int sum = 0;
    if (&load)
    {
        char word[LENGTH + 1];
        const char *dictionary;
        FILE *dictionary_file = fopen(dictionary, "r");
        while (fscanf(dictionary_file, "%s", word) != EOF)
        {
            sum++;
        }
    }
    return sum;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    char word[LENGTH + 1];
    node *head = hash_table[hash(word)];
    node *cursor = head;

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

    if (cursor == NULL)
    {
        return true;
    }
    else
    {
       return false;
    }
}
//实现字典的功能
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“dictionary.h”
//单词的最大长度
#定义长度45
//定义结构节点
类型定义结构节点
{
字符字[长度+1];
结构节点*下一步;
}
节点;
//散列函数
无符号整数散列(常量字符字[长度+1])
{
intx=atoi(&word[0]);
int-y;
if(isupper(字[0]))
{
y=(x+13)%26;
}
if(islower(字[0]))
{
y=(x+7)%26;
}
if(isalpha(字[0])==0)
{
返回0;
}
返回y;
}
//创建链接列表的哈希表
节点*哈希表[26];
//如果单词在字典中,则返回true;否则返回false
布尔检查(常量字符*单词)
{
node*head=hash_表[hash(word)];
节点*光标=头部;
while(光标!=NULL)
{
if(strcasecmp(字、光标->字)==0)
{
返回true;
}
光标=光标->下一步;
}
返回false;
}
//将字典加载到内存中,如果成功则返回true,否则返回false
布尔加载(常量字符*字典)
{
字符字[长度+1];
FILE*dictionary_FILE=fopen(dictionary,“r”);
if(dictionary_file==NULL)
{
卸载();
返回false;
}
while(fscanf(字典文件,“%s”,word)!=EOF)
{
//为节点分配内存
node*new_node=malloc(sizeof(node));
if(新节点==NULL)
{
卸载();
返回false;
}
//将节点插入到链接列表中
int索引=散列(字);
node*head=hash_表[hash(word)];
//在节点中放置单词
strcpy(新节点->单词,单词);
//连接节点
新建节点->下一步=头部;
哈希表[索引]=新的\u节点;
}
fclose(字典文件);
返回true;
}
//如果已加载,则返回字典中的字数;如果尚未加载,则返回0
无符号整数大小(void)
{
整数和=0;
如果(&load)
{
字符字[长度+1];
常量字符*字典;
FILE*dictionary_FILE=fopen(dictionary,“r”);
while(fscanf(字典文件,“%s”,word)!=EOF)
{
sum++;
}
}
回报金额;
}
//从内存中卸载字典,如果成功则返回true,否则返回false
bool卸载(无效)
{
字符字[长度+1];
node*head=hash_表[hash(word)];
节点*光标=头部;
while(光标!=NULL)
{
节点*温度=光标;
光标=光标->下一步;
免费(临时);
}
if(游标==NULL)
{
返回true;
}
其他的
{
返回false;
}
}
当我通过GDB运行它时,我得到以下消息,指示分段错误:

Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=0x7ffff728d760 <main_arena>, bytes=56) at malloc.c:3777
3777    malloc.c: No such file or directory.
程序接收信号SIGSEGV,分段故障。
_位于malloc.c:3777的int_malloc(av=0x7ffff728d760,字节=56)
3777 malloc.c:没有这样的文件或目录。
以下是我键入“where”时的GDB响应:

malloc.c:3777处的malloc(av=0x7ffff728d760,字节=56) #1 0x00007FF6F4DAE0位于malloc.c:2893 #加载中的2 0x0000000000422abf(字典=0x382e332d6e696168) c:66 #3 0x722d72656c69706d英寸??() #4 0x61732f62696c2f74英寸??() #5 0x5f72657a6974696e英寸??() #6 0x732f6e6f6d6d6f63英寸??() #7 0x72657a6974696e61英寸??() #8 0x632e7367616c665f英寸??() #9 0x6165720000000063英寸??() #10 0x0063632e31720064英寸??() #11 0x28800000000000英寸??() #12 0x5F7625F706D7421英寸??() #13 0x287469427465672e英寸??() #14 0x6C002929786469英寸??() #15 0x6c6f6f742d6d766c英寸??()
有人知道为什么会出现分割错误吗?当我通过valgrind运行程序时,系统指示没有内存泄漏。有人知道如何解决这个问题吗?

load()
函数中有几个问题,但我不确定他们是否对分段错误负责

  • 在从
    word
    复制到
    new\u node->word
    之前,您正在调用
    hash(new\u node->word)
    。你应该改为散列
    word

  • 实际上,您从未将新节点放入哈希表中。在将新节点链接到旧节点头后,将其设置为
    head
    ,但哈希表仍指向旧节点头

  • 更新代码:

    // Loads dictionary into memory, returning true if successful else false
    bool load(const char *dictionary)
    {
        char word[LENGTH + 1];
        FILE *dictionary_file = fopen(dictionary, "r");
        if (dictionary_file == NULL)
        {
            unload ();
            return false;
        }
    
        while (fscanf(dictionary_file, "%s", word) != EOF)
        {
            //allocate memory for node
            node *new_node = malloc(sizeof(node));
            if (new_node == NULL)
            {
                unload();
                return false;
            }
    
            //insert node into linked list
            int hashcode = hash(word);
            node *head = hash_table[hashcode];
    
            //place word in node
            strcpy(new_node->word, word);
    
            //connect nodes
            new_node->next = head;
            hash_table[hashcode] = new_node;
        }
        fclose(dictionary_file);
        return true;
    }
    
    我在您发布的代码中没有看到任何会导致分段错误的内容,因此您可能在其他一些未包含的代码中有未定义的行为。使用类似于
    valgrind
    的工具帮助查找它。

    关于:

    const char *dictionary;
    FILE *dictionary_file = fopen(dictionary, "r");
    
    (可能是seg故障的原因)

    指针
    字典
    在传递到
    fopen()

    因此,代码试图从内存中的某个“随机”位置读取某个“随机”字节数(直到遇到NUL字节),以获取要打开的文件的名称

    OT:调用C库函数时,如
    fopen()
    ,始终在调用后检查是否有错误


    编译时,始终启用警告,然后修复这些警告。(对于
    gcc
    ,至少使用:
    -Wall-Wextra-Wconversion-pedantic-std=gnu11

    哈希表是指针数组。你有没有将它们初始化为指向节点?请给我们看一个完整的程序。您问题中当前的代码没有
    #include
    指令和
    主功能。请阅读:顺便说一句,“没有这样的文件或目录”消息是不相关的。这只是意味着您的程序在运行库中崩溃,gdb无法查看库源代码。这是相关的分段错误——gdb应该能够告诉您它发生在您自己的代码中的什么地方。(在收到该消息后,请尝试使用
    where
    命令。)在malloc()内部崩溃,i
    const char *dictionary;
    FILE *dictionary_file = fopen(dictionary, "r");