Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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
C 释放(数据)尝试释放错误的地址_C_Heap_Free - Fatal编程技术网

C 释放(数据)尝试释放错误的地址

C 释放(数据)尝试释放错误的地址,c,heap,free,C,Heap,Free,我实现了一个树,其节点如下所示: typedef struct node_s { struct node_s *parent; struct node_s *left; struct node_s *right; data_t *data; }node_t; 使用*data指向数据结构,并使用键对树进行排序 当我添加一个新节点时,它如下所示: node_t *add_Item (node_t *root){ //root is the root of the tree

我实现了一个树,其节点如下所示:

typedef struct node_s {
   struct node_s *parent;
   struct node_s *left;
   struct node_s *right;
   data_t *data;
}node_t;
使用
*data
指向数据结构,并使用
键对树进行排序

当我添加一个新节点时,它如下所示:

node_t *add_Item (node_t *root){  //root is the root of the tree 

    ...

    data_t *new_data;
    new_data = calloc(1,sizeof(data_t));
    if(!new_data){
        ...    //Error handling
    }
    root = insert_Node(root, new_data);
    ... //modifying new_data

    return root;
}
我不明白的是,
免费(临时数据)
不起作用。此外,调试信息对我来说没有意义:

  • temp_data=0x0000024C856DA950
  • root->data=0x0000024C856DA9D0
  • 调试消息:
    HEAP[Program.exe]:为RtlValidateHeap(0000024C856D0000,0000024C856DA920)指定的地址无效。
    错误消息中的地址总是
    0x30
    小于
    temp\u data
只要它能帮助任何人:Windows10,VisualStudio

编辑-最小可复制示例

#include <stdio.h>
#include <stdlib.h>

typedef struct data_s {
    long key;
}data_t;

typedef struct node_s {
    struct node_s* parent;
    struct node_s* left;
    struct node_s* right;
    data_t* data;
}node_t;


node_t* init_node(data_t* data) {
    node_t* new_node;
    if ((new_node = calloc(1, sizeof(node_t)))) {
        new_node->data = data;
        fprintf(stderr, "node created\n");
    }
    return new_node;
}

node_t* insert_node(node_t* root, data_t* data) {

    node_t* node;

    if (root) {
        if (root->data->key == data->key) {
            printf("This item alreay exists!\n%ld will be replaced!\n", data->key);
            data_t* temp_data;
            temp_data = root->data;
            root->data = data;
            free(temp_data);

        }
        else if (root->data->key > data->key) {
            node = insert_node(root->right, data);
            root->right = node;
            node->parent = root;
        }
        else {
            node = insert_node(root->left, data);
            root->left = node;
            node->parent = root;
        }

    }
    else {
        root = init_node(data);
    }
    return root;
}


int main(int argc, char** argv) {

    data_t* data;
    node_t* root = 0;

    while (1) {

        if (!(data = calloc(1, sizeof(data_t)))) {
            return(EXIT_FAILURE);
        }
        printf("What is the key for the new data?\n");
        scanf("%ld", &(data->key));
        root = insert_node(root, data);

        //there would be more editing of data afterwards
    }

    return(EXIT_SUCCESS);
}
#包括
#包括
类型定义结构数据{
长键;
}数据;
类型定义结构节点{
结构节点\父节点;
结构节点(左);
结构节点(右);
数据_t*数据;
}节点t;
node_t*init_node(data_t*data){
node_t*新节点;
if((new_node=calloc(1,sizeof(node_t))){
新建_节点->数据=数据;
fprintf(stderr,“创建的节点”);
}
返回新的_节点;
}
节点\u t*插入\u节点(节点\u t*根,数据\u t*数据){
node_t*node;
如果(根){
如果(根->数据->键==数据->键){
printf(“此项确实存在!\n%ld将被替换!\n”,数据->键);
数据*温度数据;
临时数据=根->数据;
根->数据=数据;
免费(临时数据);
}
否则如果(根->数据->键->数据->键){
节点=插入_节点(根->右侧,数据);
根->右=节点;
节点->父节点=根节点;
}
否则{
节点=插入_节点(根->左,数据);
根->左=节点;
节点->父节点=根节点;
}
}
否则{
根=初始节点(数据);
}
返回根;
}
int main(int argc,字符**argv){
数据_t*数据;
node_t*root=0;
而(1){
如果(!(data=calloc(1,sizeof(data_t))){
返回(退出失败);
}
printf(“新数据的键是什么?\n”);
scanf(“%ld”&(数据->键));
根=插入节点(根,数据);
//之后会有更多的数据编辑
}
返回(退出成功);
}

这似乎可行,但我不知道为什么这个可行,而我的其他程序不行。

请发布。这段代码似乎并不正确,因为您的
temp_data
属于
data_t
类型,它似乎是非指针类型,但我们无法知道。乍一看,我认为您的insert函数正在返回节点的根,您正在尝试释放它,而不是节点内的数据指针。这似乎与函数的轻微偏移相匹配。@eugene你说得对,它在堆栈上分配的实际代码
数据中是正确的。您只能释放使用
*alloc()
分配的数据。您最初是如何分配root的?
#include <stdio.h>
#include <stdlib.h>

typedef struct data_s {
    long key;
}data_t;

typedef struct node_s {
    struct node_s* parent;
    struct node_s* left;
    struct node_s* right;
    data_t* data;
}node_t;


node_t* init_node(data_t* data) {
    node_t* new_node;
    if ((new_node = calloc(1, sizeof(node_t)))) {
        new_node->data = data;
        fprintf(stderr, "node created\n");
    }
    return new_node;
}

node_t* insert_node(node_t* root, data_t* data) {

    node_t* node;

    if (root) {
        if (root->data->key == data->key) {
            printf("This item alreay exists!\n%ld will be replaced!\n", data->key);
            data_t* temp_data;
            temp_data = root->data;
            root->data = data;
            free(temp_data);

        }
        else if (root->data->key > data->key) {
            node = insert_node(root->right, data);
            root->right = node;
            node->parent = root;
        }
        else {
            node = insert_node(root->left, data);
            root->left = node;
            node->parent = root;
        }

    }
    else {
        root = init_node(data);
    }
    return root;
}


int main(int argc, char** argv) {

    data_t* data;
    node_t* root = 0;

    while (1) {

        if (!(data = calloc(1, sizeof(data_t)))) {
            return(EXIT_FAILURE);
        }
        printf("What is the key for the new data?\n");
        scanf("%ld", &(data->key));
        root = insert_node(root, data);

        //there would be more editing of data afterwards
    }

    return(EXIT_SUCCESS);
}