1

Since in C++, NULL is essential jus 0. Every time I have a variable uninitialized set to NULL and later on I want to check whether this variable is initialized or not by comparing it to NULL. However, if the variable happens to be assigned to 0 somewhere, then there is no way I can tell whether it has been initialized or not. There is definitely some work around I can do to avoid the situation like using another wrapped object to wrap the variable or use a flag. But I am looking for a more elegant solution. For example:

int b[4] = {1,2,3,0};
int a = NULL;
int i = 0;
while(true){
    if(a == 0)return;
    a = b[i++];
}

so here it will goes into the if statement right away but I want to wait until a is assigned to 0 by b's last element

Jason Liu
  • 49
  • 6
  • 2
    You may not compare uninitialized variables because they can have so-called trap values. – Vlad from Moscow Jan 16 '17 at 21:36
  • What is your problem? Please, be more specific. NULL and 0 are the same thing. What is next? – Kirill Kobelev Jan 16 '17 at 21:36
  • 4
    *"Since in C++, NULL is essential jus 0"* Maybe, maybe not. It expands to some "nullpointer-constant", so `#define NULL nullptr` would be legal too. In particular, `int a = NULL;` may or may not compile. – Baum mit Augen Jan 16 '17 at 21:37
  • 2
    Possible dupes: https://stackoverflow.com/questions/1226489/c-check-if-integer-is-assigned https://stackoverflow.com/questions/6822044/checking-if-a-variable-is-initialized (I'm out of votes sadly.) – Baum mit Augen Jan 16 '17 at 21:40
  • 1
    By saying `int a = NULL;`, you are *initializing* `a` by assigning the value `NULL` (which *might* be `0`, but it is perfectly acceptable to not be). – Travis Gockel Jan 16 '17 at 21:42
  • 4
    `int a = NULL;` is likely to be accepted, but it's inappropriate. `NULL` is a macro that expands to a null pointer constant, which is likely to be the integer constant `0`, but it's intended to be used in a context that requires a pointer value. Since `int` is not a pointer type, `int a = 0;` is exactly equivalent and much clearer. The fact that `0` can be used as a null pointer constant is pretty much an accident of history. And the `NULL` macro isn't necessarily defined as `0`; it could be defined as `nullptr` or something else, which is *not* equivalent to `0`. – Keith Thompson Jan 16 '17 at 21:44
  • 4
    _"Looking for a more elegant solution."_ How about you guarantee you variables are initialized through better start-up design? I find "checking for initialization" is just an awful pitfall that can cause needless error checking and branch generation. – mascoj Jan 16 '17 at 21:46
  • Can you give us a realistic example of a situation where you would want to do this? – David Schwartz Jan 17 '17 at 04:23
  • I have just add a specific example – Jason Liu Jan 20 '17 at 00:06

2 Answers2

7

I think what you're looking for is boost::optional.

#include <boost/optional.hpp>

...

boost::optional<int> maybe_int;

...

if (maybe_int) // test if it has been assigned to
{
    if (maybe_int.value() == 0) // has is been set to zero?
    {
        // ...
    }
}    
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
4

I think you're making a conceptual mistake here. Even though NULL is defined as 0 somewhere, this does not mean they are the same on a conceptual level.

NULL is a pointer with (usually) the value 0. By convention, there cannot be an object at NULL. We can therefore use code such as MyClass *obj = NULL; to specify that there is no object at obj. This works because obj is a pointer.

For non-pointer types, this does not work and we need to use other tools such as arbitrarily defining one value as invalid (often -1is used for this) or using optionals.

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267