#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    1
    Rep Power
    0

    Question NULL Pointer mystery in C


    I have one problem with NULL pointer checking, i am creating one strcut pointer like this

    struct node *root;

    now i am calling one function and pass the address of this pointer to it as a parameter

    insert(&root , key);

    in this function i check if root is NULL or not?

    struct node *
    insert(struct node **n, int d)
    {
    if (!(*n))
    {
    if (!((*n) = malloc(sizeof(struct node))))
    {
    return NULL;
    }
    (*n)->left = (*n)->right = NULL;
    (*n)->d = d;
    return *n;
    }
    if (KEY(d) < KEY((*n)->d)) {
    return insert(&(*n)->left, d);
    }
    if (KEY(d) > KEY((*n)->d)) {
    return insert(&(*n)->right, d);
    }
    return NULL;
    }

    now in the very first call to this function, *n is not NULL and instead of going into if loop for memory allocation, tries to do recursion and program fails giving memory access error.
    now if i haven't assigend anything to root, how come it is not NULL?
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    now if i haven't assigend anything to root, how come it is not NULL?
    An equally valid question would be, "if I haven't assigned anything to root, how come it is not 20?"

    It's the same thing, really. Until you actually assign a value to root, its value is undefined. And quite likely anthing but NULL.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Location
    Blacksburg VA/Philly PA
    Posts
    38
    Rep Power
    12
    you could always try passing the node normally, and then using "this" (without qoutes of course). this is a pointer to any data structure. it is automatically assigned to any data structure, even if it is user defined.


    pass the node by reference and then try

    if(this == NULL)
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    1
    Rep Power
    0
    you cannot assume that ' struct node *root; ' will initialize root to NULL. since it is an uninitialized pointer you need to explicitly assign NULL to it before going fwd.

    hope that helps.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    m shah,

    You might try a little switch in how you allocate memory for structures. When you allocate a block of memory, instead of using malloc() try calloc(), which initializes it's memory to 0.

    I now use calloc exclusively, and I have a lot fewer crashes. After that you only need to make sure that you're getting the right size, and your code will be quite trouble-free.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo