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...
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.
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.
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).
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).
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.
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....
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).
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.
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:
Bookmarks