简体   繁体   中英

filling a binary tree function in c

I have a problem with adding data to my tree using this function. I am using codeblocks and when I run my program it gives me windows error box

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

struct arb{
    int data;
    struct arb*FG;
    struct arb*FD;
};

void remplit(struct arb*r,int i)
{
    if (r==NULL)
    {
        r=malloc(sizeof(struct arb));
        r->data=i;
        r->FD=NULL;
        r->FG=NULL;
    }
    else
    {
        if (i>(r->data))
        {
            remplit(r->FD,i);
        }
        else
        {
            remplit(r->FG,i);
        }
    }
}

struct arb * test=NULL;

int main()
{
    remplit(test,5);
    printf("%d",test->data);
    return 0;
}

You have a global pointer set to NULL. You then pass that pointer by value to another function that allocates dynamic memory to it with malloc .

The problem is that because the pointer is passed by value, the value of the global is unchanged (is still NULL) because the copy of your pointer now stores the address of the memory region allocated by malloc and not your global pointer.

Now when you dereference the pointer (after the call to remplit ) it is giving you a segfault because test is still NULL.

If you want to use a function to allocate memory to a pointer that you pass to it, you need to make the function take a double pointer and assign the return value of malloc to the dereference of the pointer.

As a simple example, consider the following for creating a char array using double pointer and a utility function that does the allocation

void create_char_array(char** p, int size)
{
    *p = NULL;
    *p = malloc(size * (sizeof char));
    /* *p now points to dynamically allocated memory */
}

int main()
{
    char* my_array;
    /* allocate memory for an array of 10 chars using above function
       by passing the address of my_array */
    create_char_array(&my_array,10);
    if (my_array != NULL)
    {
        /* you can now safely assign values to valid the indices in the array */
    }
    /* release memory */
    free (my_array);
    return 0;
}

If you learn to use a debugger, you can step through your code at all steps. You should see that inside your function the memory is allocated to your r pointer (which is a copy of test ), but that the value of the global test pointer is still NULL. Your code actually has a memory leak because the copy of the pointer inside your function is destroyed when the function exits. Memory has been allocated to it but there is no way to free it as the variable no longer exists.

You are passing your pointer by value, not name.

remplit(test,5);

This sends the value of test to remplit .

r=malloc(sizeof(struct arb));

This points r , a local variable, to the allocated memory. This doesn't effect the value of test in main .

printf("%d",test->data);

test is still NULL , your attempt to de-reference it causes a seg fault.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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