Thread: NULL-pointers

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    NULL-pointers

    Hey!
    I have just malloc'd a chunk of memory for a struct with various members.
    Some of those members is pointers.

    I want to check if the pointer isnt given a value by doing so:
    Code:
    if(structPtr->ptr == NULL) ...;
    Is it enough to zero-fill the memory, and assume that a zerofilling means the ptr will be NULL?

    In other words, is it safe to assume that NULL == 0(or something similar, like (void *) 0)?
    Or can NULL be implemented to be -1 or something like that?
    Last edited by Drogin; 10-11-2009 at 09:49 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Drogin View Post
    Is it enough to zero-fill the memory, and assume that a zerofilling means the ptr will be NULL?
    Yes. As you know, you cannot do this:
    Code:
    typedef struct {
    	char *ptr = NULL;
    } this;
    But this will set eg.ptr to NULL:
    Code:
    this eg = {0};

    Beware King Mir's caveat tho.
    Last edited by MK27; 10-11-2009 at 10:33 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Drogin View Post
    Hey!
    I have just malloc'd a chunk of memory for a struct with various members.
    Some of those members is pointers.

    I want to check if the pointer isnt given a value by doing so:
    Code:
    if(structPtr->ptr == NULL) ...;
    Is it enough to zero-fill the memory, and assume that a zerofilling means the ptr will be NULL?

    In other words, is it safe to assume that NULL == 0(or something similar, like (void *) 0)?
    Or can NULL be implemented to be -1 or something like that?
    NULL can theoretically be implemented as something like -1, so using memset or similar functions will technically not guarantee null initialization. However, if you explicitly set the pointers to 0, then it is guaranteed to work, because a 0 in a pointer context will always be the same as NULL.

    Code:
    So initializing a struct with {0} will always work.
    Practically I've never heard of a compiler that does not implement NULL as 0. Checking for 0 is such a common operation even not counting pointers, that architectures tend to make a separate optimized instruction. Checking for -1 is not so common.

    The same rule applies to floating point numbers.
    Last edited by King Mir; 10-11-2009 at 10:26 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM