我的代码或Netbeans自行更改树的内存地址的值(C)

问题描述 投票:0回答:1

[HELLO FOLKS,这里是我从代码中得到的一个奇怪的错误。

我正在建立一个记录苍蝇的二叉搜索树。

所以结构是这样构建的

struct SVolo{
       float orariopart;
       float orarioarr;
       char pospartenza[MAX];
       char posarrivo[MAX];
       char code[COD];
       int nposti;
       bool *mappa_posti;

   };

struct SNode{
   TVolo info;
   struct SNode *left;
   struct SNode *right;

};

typedef struct SVolo TVolo;
typedef struct SNode TNode;
typedef TNode *TBST;

所以我做了将苍蝇插入树中的功能

TBST bst_insert(TBST tree, TVolo info){
    if(tree==NULL){
        tree = Create_Node(info);
        return tree;
    }
    if( info_greater(info, tree->info) ){
        tree->right = bst_insert(tree->right, info);
        return tree;
    }else{
        tree->left = bst_insert(tree->left, info);
        return tree;
    }
}

我在其中提供苍蝇信息,如果代码(字母数字)更大,则树按代码组织,将其插入右侧子树中,否则向左移动。

我调试了,看来插入很好

当我使用该功能打印时:

void bst_print(TBST tree){
    if(tree == NULL){
        return;
        }
        bst_print(tree->right);
        info_print(tree->info);
        bst_print(tree->left);
}

它在第一个bst_print(tree->right);中,并检查它是否为null。然后退出“循环”,在该“循环”中,我们将tree-> right作为子树传递。然后它打印根的信息。最后,当它要做bst_print(tree->left)时,给了我一个转储核心错误。分段错误。

我使用了调试程序,我注意到左子树的地址不为null。即使在Create_Node中,它也会将其初始化为NULL。

它具有以下值0x213,但我从未触摸过其值更改过的值。Ofc给我错误,它要求打印一些未保存在子函数info_print中的内容。事实是我知道错误在哪里,但为什么会发生?还有我该如何解决?

TNode *Create_Node(TVolo info){
    TNode *New_Node=(TNode*)malloc(sizeof(TVolo));
    New_Node->info=info;
    New_Node->left=NULL;
    New_Node->right=NULL;
    return New_Node;
}

他如何创建新节点

[HELLO FOLKS,这里是我从代码中得到的一个奇怪的错误。我正在建立一个记录苍蝇的二叉搜索树。因此该结构是按以下结构构建的:SVolo {float orariopart; ...

c tree binary-search-tree
1个回答
0
投票

函数Create_Node中存在一个错误,其中我分配了sizeof(TVolo),这不是您应该分配的正确大小。用malloc(sizeof(TNode)纠正该问题后,问题就解决了。

© www.soinside.com 2019 - 2024. All rights reserved.