C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 6th, 2002, 10:02 AM
m shah m shah is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 1 m shah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #2  
Old December 6th, 2002, 10:26 AM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
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/

Reply With Quote
  #3  
Old December 7th, 2002, 12:50 PM
pschmerg pschmerg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38 pschmerg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m
Reputation Power: 11
Send a message via AIM to pschmerg
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)

Reply With Quote
  #4  
Old December 13th, 2002, 02:48 PM
tcangley tcangley is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 1 tcangley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old December 16th, 2002, 02:00 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > NULL Pointer mystery in C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap