The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
NULL Pointer mystery in C
Discuss NULL Pointer mystery in C in the C Programming forum on Dev Shed. NULL Pointer mystery in C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 6th, 2002, 10:02 AM
|
|
Junior Member
|
|
Join Date: Dec 2002
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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?
|

December 6th, 2002, 10:26 AM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
Quote: | 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/
|

December 7th, 2002, 12:50 PM
|
|
Contributing User
|
|
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38
Time spent in forums: 2 h 29 m
Reputation Power: 11
|
|
|
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)
|

December 13th, 2002, 02:48 PM
|
|
Junior Member
|
|
Join Date: Dec 2002
Posts: 1
Time spent in forums: < 1 sec
Reputation 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.
|

December 16th, 2002, 02:00 PM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
|
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/
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|