Is Null Reference Possible

Is null reference possible?

References are not pointers.

8.3.2/1:

A reference shall be initialized to
refer to a valid object or function.
[Note: in particular, a null reference
cannot exist in a well-defined
program, because the only way to
create such a reference would be to
bind it to the “object” obtained by
dereferencing a null pointer, which
causes undefined behavior. As
described in 9.6, a reference cannot
be bound directly to a bit-field. ]

1.9/4:

Certain other operations are described
in this International Standard as
undefined (for example, the effect of
dereferencing the null pointer)

As Johannes says in a deleted answer, there's some doubt whether "dereferencing a null pointer" should be categorically stated to be undefined behavior. But this isn't one of the cases that raise doubts, since a null pointer certainly does not point to a "valid object or function", and there is no desire within the standards committee to introduce null references.

Why does this code give a Possible null reference return compiler warning?

The nullable flow analysis tracks the null state of variables, but it does not track other state, such as the value of a bool variable (as isNull above), and it does not track the relationship between the state of separate variables (e.g. isNull and _test).

An actual static analysis engine would probably do those things, but would also be "heuristic" or "arbitrary" to some degree: you couldn't necessarily tell the rules it was following, and those rules might even change over time.

That's not something we can do directly in the C# compiler. The rules for nullable warnings are quite sophisticated (as Jon's analysis shows!), but they are rules, and can be reasoned about.

As we roll out the feature it feels like we mostly struck the right balance, but there are a few places that do come up as awkward, and we'll be revisiting those for C# 9.0.

Why is there no NULL reference in C++?

Because a reference carries the semantic that it points to a valid memory address that never changes; i.e. that dereferencing it is safe/defined and so no NULL checks are required. References cannot be reassigned by design.

You use a pointer when the var can be NULL and client code has to handle that case. You use a reference when you can guarantee a valid/initialised memory address.

One example of using pointers is as a member of a class to store a "reference" to some instance that might not be known or able to be initialised at class construction time. However, member references must be initialised at construction time (via initialiser lists) and their assignment cannot be deferred.

If you allow a null reference it is then no different to a pointer besides syntax (the same NULL checks would need to take place.)

Update:

"And, in most OOP languages, objects can be NULL - Pascal, C#, Java, JavaScript, PHP, etc. [...] So why is C++ somehow special and doesn't have a NULL object? Was it just an overlook or an actual decision?"

I think you are a bit confused about this. Java and C# etc. might give the impression of "NULL objects", but these object references are more or less like a C++ pointer with simpler syntax, GC instrumentation and exception throwing. In those languages if you operate on a "Null Object" you will get some kind of exception like NullReferenceException (C#). Hell, in Java its called a NullPointerException.

You have to check for null before you can use them safely. Kind of like C++ pointers (except in most managed languages, pointers are initialised to NULL by default, whereas in C++ its usually up to you to take care of setting the initial pointer value (otherwise undefined/whatever memory was already there)).

The C++ view is about having choice, and hence being verbose:

  • Use a plain pointer to do how you please, checking NULL where necessary.
  • Use references which have a compiler-enforced validity semantic/constraint.
  • Roll your own smart pointers that do bookkeeping and behave whichever way you want them to.
  • Using void pointers (cautiously!) to reference an untyped block of memory if ever required.

How do I test whether a reference is NULL?

In a well-formed C++ program, references are never NULL (more accurately, the address of an object to which you have a reference may never be NULL).

So not only is the answer "no, there's no way", a corollary is "this makes no sense".


Your statement regarding C makes no sense either, since C does not have references.

And as for Java, its "references" are more like C++ pointers in many ways, including this one.

Comparing such specific behaviours between different languages is something of a fool's errand.


If you need this "optional object" behaviour, then you're looking for pointers:

std::string xxx(const NotMyClass* ptr) {
if (ptr == NULL)
throw SomeException();

const NotMyClass& ref = *ptr;
/* ... */
}

But consider whether you really need this; a decent alternative might be boost::optional if you really do.

A reference can not be NULL or it can be NULL?

Saying person &object1=*object is not the same thing as saying person &object1=NULL. Probably the compiler is just not smart enough to find out that you are dereferencing null pointer, but you'll get a runtime error anyway. So they are kind of true still ;)

Reference pointing to a Null-object

I was surprised that no one talked about when reference can point to a null object

That's because it can't, in a correct program.

Your function that dereferences a nullpointer has Undefined Behavior. The compiler is allowed to emit code that, for example, causes a crash at that point.

However, one possible effect of UB is that the code does what one thought it would do. So null-references can occur. I have never encountered one, but if you do, then it means that there is a serious logic error in the code.

All uses of the null-object-reference function you show, are logic errors.

You'd better grep up those uses and fix things. ;-)

Cheers & hth.,

Initializing a reference to member to NULL in C++

No, references cannot be NULL in C++.1

Possible solutions include:

  • using a pointer instead of a reference.
  • having a dummy Object instance that can be used to indicate "no object".

[1] From the C++11 standard:

[dcl.ref] [...] a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.

possible null reference return c# linq

SingleOrDefaultAsync() does exactly what's in the method name, it tries to find a single entry and returns the default if nothing is found.

The default for a reference type, you object Pie in this case is null hence the warning.

You can either return Task<Pie?> or instead handle the null value in some way. One way would be to use .SingleAsync() instead, which will throw if nothing was found - but therefore it will never return null.



Related Topics



Leave a reply



Submit