-
May 6th, 2009, 02:46 PM
#1
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...
-
May 6th, 2009, 02:59 PM
#2
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
-
May 6th, 2009, 03:15 PM
#3
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.
-
May 6th, 2009, 03:17 PM
#4
Re: NULL Values
Originally Posted by Joeman
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.
-
May 6th, 2009, 03:32 PM
#5
Re: NULL Values
Why not just add a "valid" boolean field to whatever structure you're working with?
-
May 6th, 2009, 03:38 PM
#6
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
-
May 6th, 2009, 03:43 PM
#7
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).
-
May 6th, 2009, 04:37 PM
#8
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).
-
May 6th, 2009, 04:46 PM
#9
Re: NULL Values
Originally Posted by BleaS
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.
-
May 6th, 2009, 04:49 PM
#10
Re: NULL Values
Originally Posted by BleaS
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....
-
May 6th, 2009, 05:44 PM
#11
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).
-
May 6th, 2009, 08:17 PM
#12
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.
-
May 7th, 2009, 03:41 AM
#13
Re: NULL Values
Originally Posted by JVene
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.
-
May 7th, 2009, 06:33 AM
#14
Re: NULL Values
boost :: optional is what you´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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|