Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cs50 pset5卸载问题-内存泄漏_C_Cs50 - Fatal编程技术网

cs50 pset5卸载问题-内存泄漏

cs50 pset5卸载问题-内存泄漏,c,cs50,C,Cs50,我正在尝试解决cs50 pset5。我正在使用一种新的方法 我可以成功地将单词插入到文档中。但是,当我卸载内存时,它失败了。我在卸载问题上被困了好几天 当我尝试卸载函数时,它声明Erorr消息:双重释放或损坏(!prev):0x000000000205d010*** 这可能是由于我的卸载功能,但我已经画出了逻辑。我觉得很好。有人知道该在哪里修改吗 #include <stdio.h> #include <stdlib.h> #include <stdbool.h&g

我正在尝试解决cs50 pset5。我正在使用一种新的方法

我可以成功地将单词插入到文档中。但是,当我卸载内存时,它失败了。我在卸载问题上被困了好几天

当我尝试卸载函数时,它声明Erorr消息:双重释放或损坏(!prev):0x000000000205d010***

这可能是由于我的卸载功能,但我已经画出了逻辑。我觉得很好。有人知道该在哪里修改吗

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

#define LENGTH 45
#define SIZE_OF_CHILD 27

typedef struct Node {
    bool is_word;
    struct Node* child[SIZE_OF_CHILD];
} Node;


Node* root;
Node* temp;

Node* create_node(Node* node);
void test();
int cal_key(char c);
Node* insert(const char* word);
void unload(Node* node);

int main() {
    root = malloc(sizeof(Node));
    if (root == NULL) {
        exit(0);
    }

    root = create_node(root);
    temp = malloc(sizeof(Node));
        if (temp == NULL) {
        exit(0);
    }

    test();

}

// **************** function to create a node *******************
Node* create_node(Node* node) {
    node->is_word = false;
    for (int i = 0; i < SIZE_OF_CHILD; i++) {
        node->child[i] = NULL;
    }
    return node; 
}

//************* calculate the key value ************
// Assume that the input is in lower case
int cal_key(char c) {
    if (isalpha(c)) {
        if (islower(c)) {
             return c - 'a';
        }
        else {
            return c + 32 -'a';
        }
    }
    else {
        return SIZE_OF_CHILD - 1;
    }
}

//*************** function to insert an item in the the node ***********
Node* insert(const char* word) {
    int str_len = strlen(word);
    temp = root;
    int key;
    for (int i = 0; i < str_len; i++) {
        key = cal_key(word[i]);
        if (temp->child[key] == NULL) {
            Node* node = malloc(sizeof(Node));  
            if (node == NULL) {
                fprintf(stderr, "Error in creating node\n");
                exit(0);
            }
            node = create_node(node);
            temp->child[key] = node;
        }

        temp = temp->child[key];
    }
    temp->is_word = true;
    return root;
}


//***************** function to unload a function ********************
void unload(Node* node) {
    // This is to find the last node
    for (int i = 0; i < SIZE_OF_CHILD; i++) {
        if (node->child[i] != NULL) {
            unload(node->child[i]);
        }
    free(node);
    }
}

void test() {
    root = insert("Peter");
    unload(root);
    }
#包括
#包括
#包括
#包括
#包括
#定义长度45
#定义子项27的大小
类型定义结构节点{
布尔是一个词;
结构节点*子节点[子节点的大小];
}节点;
节点*根;
节点*温度;
节点*创建_节点(节点*节点);
无效试验();
int cal_键(字符c);
节点*插入(常量字符*字);
无效卸载(节点*节点);
int main(){
root=malloc(sizeof(Node));
if(root==NULL){
出口(0);
}
root=创建_节点(root);
temp=malloc(sizeof(Node));
if(temp==NULL){
出口(0);
}
test();
}
//*************函数创建一个节点*******************
节点*创建_节点(节点*节点){
node->is_word=false;
for(int i=0;i<子对象的大小;i++){
节点->子[i]=NULL;
}
返回节点;
}
//*************计算键值************
//假设输入是小写的
int cal_键(字符c){
if(isalpha(c)){
if(岛下(c)){
返回c-‘a’;
}
否则{
返回c+32-'a';
}
}
否则{
返回子元素的大小\u-1;
}
}
//***************函数在节点中插入项***********
节点*插入(常量字符*字){
int str_len=strlen(字);
温度=根;
int键;
对于(int i=0;i子[键]==NULL){
Node*Node=malloc(sizeof(Node));
if(node==NULL){
fprintf(stderr,“创建节点时出错”);
出口(0);
}
节点=创建_节点(节点);
temp->child[键]=节点;
}
temp=temp->child[key];
}
temp->is_word=true;
返回根;
}
//*****************函数来卸载函数********************
无效卸载(节点*节点){
//这是为了找到最后一个节点
for(int i=0;i<子对象的大小;i++){
如果(节点->子[i]!=NULL){
卸载(节点->子节点[i]);
}
自由(节点);
}
}
无效测试(){
根=插入(“彼得”);
卸载(根);
}

您正在循环中释放
节点
子节点的大小时间。OMG!!谢谢!这解决了我的问题questions@EugeneSh. 我想问一些后续问题。程序现在可以编译了。然而,当我使用valgrind时,它说我没有释放temp的内存。当我试着把temp放在main的末尾时,它说double-free或corruption(out)。有什么建议吗?您将root分配给temp,因此您丢失了对它的引用,无法再释放它,root和temp是分配后的别名,指向同一内存地址。如果你同时释放它们,你会得到一个双重释放错误。如果我将temp分配给root,这是否意味着我不需要一个malloc作为temp,因为temp只是根的一个镜像,它已经被malloc-ed了。我这样想对吗?