【问题标题】:Malloc assertion failed in CMalloc 断言在 C 中失败
【发布时间】:2023-04-04 08:05:01
【问题描述】:

我正在学习 cs50x 课程,做拼写检查程序。在我第四次执行这个程序时,我遇到了 malloc 问题。 这次我决定实现一个二叉树。 我已经阅读了很多关于这个问题的线程并多次检查了我的代码,但我仍然无法理解我做错了什么。 问题出现在将字典加载到内存中的递归函数中。

#include <stdbool.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "dictionary.h"



// standart node of the trie
typedef struct node
{
    char word[LENGTH + 1];
    struct node* less;
    struct node* more;
}
node;

// Function definitions
void unload_node(node* pr_node);
void ld_bin_tree(int min, int max, node* node);
bool check_word(char* lword, node* parent);

// Global variables
// root of the tree
node* root;
FILE* dict;
//size of dictionary
int dict_size = 0;

bool load(const char* dictionary)
{
    // open dictionary file 
    dict = fopen(dictionary, "r");
    int nwords = 0;
    int min = 0;
    int max = 0;
    root = malloc(sizeof(node));

    //if file wasn't open
    if(dict == NULL)
    {
        printf("Error opening ditionary file!");
        return false;
    }

    // tmp storage for read word
    char buffer[LENGTH + 1];

    // count words in the dictionary
    while(fscanf(dict, "%s", buffer) > 0)
    {
        nwords++;
    }
    max = nwords;
    rewind(dict);
    ld_bin_tree(min, max, root);


    // close file
    fclose(dict);
    return false;
}
/*
 * Recursion function to fill in binary tree
 */

void ld_bin_tree(int min, int max, node* node)
{
    // tmp word holder
    char buffer[LENGTH + 1];

    // next mid value
    int mid = (min + max) / 2;

    // if mid == 0 then the bottom of the brunch reached, so return
    if(max - min < 2)
    {
        if(min == 0)
        {
            fscanf(dict, "%s", node->word);
            dict_size++;
            return;
        }
        return;
    }

    // go through the dict to the mid string
    for(int i = 0; i <= mid; i++)
    {
        fscanf(dict, "%s", buffer);
    }

    // fill in word 
    strcpy(node->word, buffer);
    // go at the beginning of the dict
    rewind(dict);

    // fill in input node
    // fill in new children nodes
    struct node* new_node = malloc(sizeof(node));

    node->less = new_node;

    // send lesser side
    ld_bin_tree(min, mid, node->less);

    new_node = malloc(sizeof(node));
    node->more = new_node;
    // send greater side
    ld_bin_tree(mid, max, node->more);

    dict_size++;
    return;
}

我尝试使用 valgrind 解决此错误,但它给了我很多关于在未分配的内存块中读取和写入的警告。但是因为我还不太擅长编程,所以这个警告并没有让我知道发生了什么。

因此,如果可能的话,我正在寻求更准确的帮助。提前谢谢你。

拼写程序的其他部分可以在这里找到: https://www.dropbox.com/sh/m1q1ui2g490fls7/AACnVhjjdFpv1J0mUUhY2uV2a?dl=0

【问题讨论】:

    标签: c malloc valgrind


    【解决方案1】:

    在函数ld_bin_tree() 中你有

    struct node* new_node = malloc(sizeof(node));
    

    这里node 是一个指针,而不是struct node 类型的对象。

    你有

    node *node;

    所以node 的全局定义被覆盖,这使它成为一个指针。

    所以你没有为你的整个结构分配内存。你应该有

    struct node* new_node = malloc(sizeof(struct node));
    

    【讨论】:

    • 啊,是的。 node* node 是一个讨厌的声明,因为它重新定义了 node 的含义。
    • 或者只写malloc(sizeof(*new_node)); 可以修复或暴露此类错误。
    猜你喜欢
    • 1970-01-01
    • 2011-02-28
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多