繁体   English   中英

为什么指针的值在打印后会发生变化?

[英]Why does the value of a Pointer change after a print?

#include <stdio.h>

#include "sorted_tree.h"

int insert_value(int value, struct TreeNode *n){
    if(value<n->value){
        if(n->left_child==NULL){
            struct TreeNode t={0};
            struct TreeNode *tpointer=&t;
            tpointer->value=value;
            tpointer->left_child=NULL;
            tpointer->right_child=NULL;
            n->left_child=tpointer;
            printf("links eingefügt\n");
        }else{
            insert_value(value,n->left_child);
        }
        return 0;
    }else if(value>n->value){
        if(n->right_child==NULL){
            struct TreeNode t={0};
            struct TreeNode *tpointer=&t;
            tpointer->value=value;
            tpointer->left_child=NULL;
            tpointer->right_child=NULL;
            n->right_child=tpointer; 
            printf("rechts eingefügt\n");        
        }else{
            insert_value(value,n->right_child);
        }
        return 0;
    }else{
        return 1;
    }
}

void print_inorder(struct TreeNode *n){
    if(n == NULL){
        printf("r");
        return;
    }
    else{
        print_inorder(n->left_child);
        printf("%d ", n->value);
        print_inorder(n->right_child);
    }
}

int main(){
    struct TreeNode t={0};
    struct TreeNode *tpointer=&t;
    tpointer->value=5;
    tpointer->left_child=NULL;
    tpointer->right_child=NULL;
    insert_value(6,tpointer);
    printf("%d",tpointer->right_child->value);
    printf("%d",tpointer->right_child->value);
}

main 中的第一个 printf() 输出正确的“6”,但第二个输出一个随机数,就好像地址已更改一样。 6 应该插入到右子节点中,所以我期望 66 作为 output。 为什么会发生这种情况,我该如何解决?

当树的第一个节点没有像在您的程序中那样动态分配时的方法

struct TreeNode t={0};
struct TreeNode *tpointer=&t;
tpointer->value=5;
tpointer->left_child=NULL;
tpointer->right_child=NULL;

不是很好。 事实上,您不能创建或使用空树。

树中的所有节点都应在需要时动态分配。

因此,对于空树,function insert_value中的指针n通常可以等于NULL 你需要检查一下。 否则这样的陈述

if(value<n->value){

可以调用未定义的行为。

同样在 function 中,您正在设置指向类型为struct TreeNode的本地 object 的指针,例如

        struct TreeNode t={0};
        struct TreeNode *tpointer=&t;

退出 function 后,本地 object t将不存在。 所以一个无效的指针将被添加到树中。

您需要动态分配一个新节点。

此外 function 实际上返回任意数字 0 或 1,因为 function 不返回 ZC1C425268E68385D1AB5074C17A94F14 的后续递归调用的最终值。

此外,在添加新节点的情况下返回 1 在逻辑上更一致,否则返回 0。

我将通过以下方式声明和定义 function

int insert_value( struct TreeNode **n, int value )
{
    if ( *n == NULL )
    {
        *n = malloc( sizeof( struct TreeNode ) );
        ( *n )->value = value;
        ( *n )->left_child  = NULL;
        ( *n )->right_child = NULL;

        return 1;
    }
    else if ( value < ( *n )->value )
    {
        return insert_value( &( *n )->left_child, value );
    }
    else if ( ( *n )->value < value )
    {
        return insert_value( &( *n )->right_child, value );
    }
    else
    {
        return 0;
    }
}

如果在 main 中有一个指向根节点的指针,声明如下

struct TreeNode *root = NULL;

然后 function 被称为

insert_value( &root, value );
 struct TreeNode t={0};
 struct TreeNode *tpointer=&t;
 tpointer->value=value;

insert_valueif/else if中的那些代码块不正确。 这是因为struct TreeNode t={0}; 创建一个仅在封闭 scope 内具有生命周期的自动变量。 在这种情况下,生命周期仅在if/else if块内。 保留对变量的引用并在此之外使用它会导致未定义的行为。

解决方案是创建生命周期超出 function 的变量。 最常见的方法是使用动态分配的 memory:

struct TreeNode *tpointer = malloc(sizeof(*tpointer));

不要忘记在不再需要时free所有动态分配的 memory。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM