简体   繁体   English

在C中动态构建二叉树时指针分配的问题

[英]Problems with pointers allocation when dynamically build a binary tree in C

studying C for few months, I encounter some difficulties with the use of pointers when dynamically building a binary tree: 学习C几个月,在动态构建二叉树时遇到一些使用指针的困难:

Given my code below: 鉴于我的代码如下:

typedef struct TNoeud
{
    int data;
    struct TNoeud *pFilsGauche;
    struct TNoeud *pFilsDroit;
} TNoeud;


void insereData(int data, TNoeud **pRacine)
{
    TNoeud *noeud=malloc(sizeof(TNoeud));

    noeud->data=data;
    noeud->pFilsDroit=NULL;
    noeud->pFilsGauche=NULL;

    while((*pRacine)!=NULL)
    {
        if(data<(*pRacine)->data)
        {
            pRacine=&(**pRacine).pFilsGauche;
        }
        else
        {
            pRacine=&(**pRacine).pFilsDroit;
        }
    }
    if(pRacine==NULL)
    {
        *pRacine=noeud;
    }
    free(noeud);
}

And in the main: 主要是:

int main(int argc, const char * argv[]) {

    TNoeud *pRacine=malloc(sizeof(TNoeud));

    pRacine->data=0;
    pRacine->pFilsGauche=NULL;
    pRacine->pFilsDroit=NULL;

    pRacine=&noeudRacine;

    insereData(4, &pRacine);

    return 0;
}

I read the following topic https://stackoverflow.com/a/28637104/7866010 for the BAD_ACCESS, but in my case, the pointer is not at NULL, as pRacine is assigned at 0. 我为BAD_ACCESS读了以下主题https://stackoverflow.com/a/28637104/7866010 ,但在我的情况下,指针不是NULL,因为pRacine被指定为0。

I read the following topic https://stackoverflow.com/a/15154553/7866010 , but it didn't help. 我阅读了以下主题https://stackoverflow.com/a/15154553/7866010 ,但它没有帮助。

I also tried the declaration variant 我也试过了声明变体

(*pRacine)->data

found in this topic https://stackoverflow.com/a/346739/7866010 without any difference. 在本主题中找到https://stackoverflow.com/a/346739/7866010没有任何区别。

So my questions are : 所以我的问题是:

  • [SOLVED with TNoeud noeud as pointer instead of local variable. [解决了TNoeud noeud作为指针而不是局部变量。 I also changed pRacine in the main the same way] Why do the pointer 我也以同样的方式更改了pRacine]为什么指针

     *pRacine == NULL 

    when I pass a pointer to an assigned value as parameter of 当我将指针传递给指定值作为参数时

     insereData(4, &pRacine) ? 
  • [SOLVED the same way] Why does the debugger give me random values to pointers [解决方法相同]为什么调试器给我指针的随机值

     [1] = 0x00007fff5fbff700) 

    and datas 和数据

     (int) data = 1606416544) 

    I didn't willingly assigned ? 我不乐意分配?

  • [SOLVED: by deleting the if(pRacine==NULL) condition and replacing it by just (*pRacine)=noeud; [已解决:删除if(pRacine==NULL)条件并将其替换为(*pRacine)=noeud; ] Now no more errors, but the result of ]现在没有更多的错误,但结果

     insereData(4, &pRacine); 

    doesn't impact pRacine : it should be 不影响pRacine:它应该是

     pRacine->pFilsDroit->data==4 

    but here it remains at NULL. 但在这里它仍然是NULL。 I don't understand why, as it's not a local variable anymore. 我不明白为什么,因为它不再是一个局部变量。

Thanks all for your answers! 谢谢大家的回答!

Some suggestions(not law). 一些建议(不是法律)。
First in order to not confuse yourself about pointers, just work with them as arrays. 首先,为了不让自己与指针混淆,只需将它们作为数组使用即可。 it really works and won't confuse you. 它确实有效,不会让你感到困惑。
For instance given pointer int* ptr , for accessing the first element go as ptr[0] . 例如,给定指针int* ptr ,用于访问第一个元素,如ptr[0]

And the problem is here 问题出在这里

if(pRacine==NULL)
    {
        *pRacine=&noeud;
    }

as the noeud is not in dynamic memory, it gets halted. 因为noeud不在动态内存中,所以它会停止。
You simply need to define the noeud as a pointer of the struct by malloc . 您只需要通过mallocnoeud定义为struct的指针。 But for memory sake, please keep an eye for free it once it's not needed. 但对于内存的缘故,请留意了free这一次它是没有必要的。

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

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