CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: NULL Values

  1. #1
    Join Date
    Feb 2009
    Posts
    135

    NULL Values

    I was wondering if there was any good reading regarding using null values in C++. Currently, in the program I'm working on for learning C++, when someone adds a person to edit values on, I have all of the values set to 0. I'd much rather have the values set to a null value. AKA they would be "initialized" but not really. I could rewrite some of the code to check for NULL values and respond appropriately in display, etc...

  2. #2
    Join Date
    Jun 2008
    Posts
    592

    Re: NULL Values

    use NULL for pointers otherwise use 0. it is just good pratice
    c++ NULL = 0
    #define NULL 0
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: NULL Values

    I have one case in my current project where I have to differentiate between initialized to 0 and not initialized when using pointers. I use 0xDEADBEEF as the uninitialized pointer.

  4. #4
    Join Date
    Feb 2009
    Posts
    135

    Re: NULL Values

    Quote Originally Posted by Joeman View Post
    use NULL for pointers otherwise use 0. it is just good pratice
    c++ NULL = 0
    #define NULL 0
    The problem with 0 in this program is that 0 can be an actual value. It's a grading program, and if I make 0 a NULL value, then it will be impossible to give a student a grade of 0.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: NULL Values

    Why not just add a "valid" boolean field to whatever structure you're working with?

  6. #6
    Join Date
    Jun 2008
    Posts
    592

    Re: NULL Values

    Perhaps I don't understand you. This could be my fault, but can you give a simple example?

    If you need to set grades to where they aren't "used" yet, why not use -1? noone can get -1 for a score
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  7. #7
    Join Date
    Nov 2006
    Posts
    1,611

    Re: NULL Values

    Stroustrup suggested to use 0 for pointers, indicating NULL was a confused issue, a vestige from C.

    BleaS: NULL, if used at all, applies to pointers, usually. It hardly ever applies to numeric values, though because it's a define, it can.

    What you're talking about is a 'non' grade value, not a null per se.

    The point regarding a status flag is a fair one, in other typical situations I've seen -1 used to indicate that a value which is otherwise always a positive quantity, including zero, is otherwise uninitialized.

    Beyond that, maximum quantities, like the maximum extent of an integer, can be used.

    Back to the flag concept, consider a class for this concept. Create a class that can operate as the numeric type you want (you can even use templates for this, so the concept is adaptable). The class would have operators for all the math/comparison/construction attributes that let it function just as the POD type it stands in for, but also includes a bool as a status flag. Assuming 'false' means uninitialized, the default constructor sets this bool to false.

    Any operation that 'initializes' this value (operator = for example) can also set this flag to true, so you don't even have to cause application code to manage the flag. "Get" functions can tell you if the value is initialized or not.

    Code:
    class V
    {
     private:
    
     int value;
     bool initialized;
    
     public:
    
     V() : value( 0 ), intialized( false ) {}
    
     bool IsNull() { return !initialized; }
    
    
     .....operators, etc....
    
    };
    This is more useful if you have lots of values, and would otherwise have lots of value/bool pairs to manage.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  8. #8
    Join Date
    Feb 2009
    Posts
    135

    Re: NULL Values

    It certainly looks like a class would be useful, but I'm not on that in my learning yet and so I suppose I will have to refer to other methods.

    I had considered using -1, but that requires signed integers, aren't integers, by default, unsigned? (For that matter, I'm actually working with doubles, not integers).

  9. #9
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    Re: NULL Values

    Quote Originally Posted by BleaS View Post
    The problem with 0 in this program is that 0 can be an actual value. It's a grading program, and if I make 0 a NULL value, then it will be impossible to give a student a grade of 0.
    I don't see the problem. As far as I know there is a difference between a pointer pointing to 0 and a pointer pointing to a memory address that contains the value 0.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: NULL Values

    Quote Originally Posted by BleaS View Post
    I had considered using -1, but that requires signed integers, aren't integers, by default, unsigned? (For that matter, I'm actually working with doubles, not integers).
    Integers are signed unless you request otherwise using the "unsigned" keyword.

    And there's no reason doubles can't be negative....

  11. #11
    Join Date
    Nov 2006
    Posts
    1,611

    Re: NULL Values

    I don't see the problem. As far as I know there is a difference between a pointer pointing to 0 and a pointer pointing to a memory address that contains the value 0.
    First off, the OP isn't actually asking about pointers - NULL in his inquiry confused me at first, too.

    He's asking about the notion of an uninitialized value, where zero is a valid, initialized quantity - but he needs to 'sense' when a value is not initialized, that is - a STATE of null, like NULL in a SQL database, as opposed to zero.


    Second: (tongue in cheek)

    A pointer is something that contains a memory location.

    Zero is never a valid memory location.

    If a pointer is pointing to a memory address that contains the value 0, the meaning is not clearly defined. What type is the pointer, or at least, of the data to which it points? How many zeros follow this first one? The two are related..

    If the memory address pointed to by the pointer has a zero, but the next byte has a 1, and the type is a 16 bit integer - then the answer as to what the value is depends on the endianness of the machine.

    However, the memory address to which the pointer points can't be zero, since zero can't be a valid address.


    In this situation, when a pointer contains a zero, it is thought of as NULL - and that's a state, not a value (though we must simply understand it as a state).

    In the OP's question, state may be independent of value. The value may be uninitialized garbage if it is known that the state is null.

    If the state is not null, the value should contain meaningful data, which includes zero.

    Hence, the suggestions of using <0 as a 'signal' of state (something we simply agree upon, like zero being state for a pointer).

    Or, the suggestions that a separate bool be used to indicate state, perhaps documenting that false indicates NULL, true indicates valid data.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  12. #12
    Join Date
    Feb 2009
    Posts
    135

    Re: NULL Values

    I thank everyone for their responses and will begin to work on shaping my program to reflect that. Though I think I'll be trying to continue on in my learning since I feel ready to do so.

  13. #13
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    Re: NULL Values

    Quote Originally Posted by JVene View Post
    In the OP's question, state may be independent of value. The value may be uninitialized garbage if it is known that the state is null.

    If the state is not null, the value should contain meaningful data, which includes zero.

    Hence, the suggestions of using <0 as a 'signal' of state (something we simply agree upon, like zero being state for a pointer).

    Or, the suggestions that a separate bool be used to indicate state, perhaps documenting that false indicates NULL, true indicates valid data.
    The best way I can think of is either a class containing a boolean determining if the variable has been initialized or not and a value. Or better, a template class would handle something like this really well, that way you only need to write 1 template class that will help you add a state property to all classes. Perhaps an interface would work too, but you wouldn't be able to use this on intergers etc.

    Heres an idea for a template. It's far from complete thought. I don't know if there is any library that provides similar functionality:

    Code:
    template <typename T> class InitSafe
    {
    public:
    	InitSafe()
    	{
    		this->Initialized = false
    	}
    	InitSafe(T t)
    	{
    		this->value = t; 
    		this->Initialized = true
    	}
    	void set(T t)
    	{
    		this->value = t; 
    		this->Initialized = true;
    	}
    	T get()
    	{
    		if(this->isInitialized())
    			return this->value;
    		else
    			throw new std::exception("Variable not initialized");		
    	}
    	bool isInitialized()
    	{
    		return this->Initialized;
    	}
    private:
    	T value;
    	bool Initialized;	
    };
    Last edited by AlastrionaAdair; May 7th, 2009 at 04:19 AM.

  14. #14
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: NULL Values

    boost :: optional is what you&#180;re looking for.
    Another way is to provide an enumeration (i.e. student grades)

    Code:
    enum GradeType_t
    {
       gtGradeA,
       gtGradeB,
       gtGradeC,
       gtGradeD,
       gtGradeE,
       gtGradeF,
       gtUnknown
    };
    Now you can initialize grades with gtUnknown and leave it there when the student did not attend the according course.
    - Guido

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured