1

studying C for few months, I encounter some difficulties with the use of pointers when dynamically building a binary tree:

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.

I read the following topic https://stackoverflow.com/a/15154553/7866010 , but it didn't help.

I also tried the declaration variant

(*pRacine)->data

found in this topic https://stackoverflow.com/a/346739/7866010 without any difference.

So my questions are :

  • [SOLVED with TNoeud noeud as pointer instead of local variable. I also changed pRacine in the main the same way] Why do the pointer

    *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;] Now no more errors, but the result of

    insereData(4, &pRacine);
    

    doesn't impact pRacine : it should be

    pRacine->pFilsDroit->data==4
    

    but here it remains at NULL. I don't understand why, as it's not a local variable anymore.

Thanks all for your answers!

2
  • 2
    TNoeud noeud; is local auto variable. Its lifespan is through the scope(only inside function). Commented Apr 16, 2017 at 20:36
  • Your variable "TNoeud noeud" disappears as soon as the function it is defined in exits. Any pointer that used to point to it now points to a black hole. Any attempt to examine such pointer leads to a violation of the laws of physics. This is all explained im any C book. Get one. Commented Apr 16, 2017 at 20:37

1 Answer 1

0

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].

And the problem is here

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

as the noeud is not in dynamic memory, it gets halted.
You simply need to define the noeud as a pointer of the struct by malloc. But for memory sake, please keep an eye for free it once it's not needed.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I edited it in my question to TNoeud *noeud=malloc(sizeof(TNoeud)); and free(noeud);.
@dxp very awesome you could fix the problem yourself, perfect. Just one note again, please have an eye over malloc all the time. becasue you need to free the allocated memory yourself once it's not required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.