Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This code appears to achieve the return of a null reference in C++

Tags:

c++

My C++ knowledge is somewhat piecemeal. I was reworking some code at work. I changed a function to return a reference to a type. Inside, I look up an object based on an identifier passed in, then return a reference to the object if found. Of course I ran into the issue of what to return if I don't find the object, and in looking around the web, many people claim that returning a "null reference" in C++ is impossible. Based on this advice, I tried the trick of returning a success/fail boolean, and making the object reference an out parameter. However, I ran into the roadblock of needing to initialize the references I would pass as actual parameters, and of course there is no way to do this. I retreated to the usual approach of just returning a pointer.

I asked a colleague about it. He uses the following trick quite often, which is accepted by both a recent version of the Sun compiler and by gcc:

MyType& someFunc(int id)
{
  // successful case here:
  // ...

  // fail case:
  return *static_cast<MyType*>(0);
}

// Use:

...
MyType& mt = somefunc(myIdNum);

if (&mt) // test for "null reference"
{
  // whatever
}
...

I have been maintaining this code base for a while, but I find that I don't have as much time to look up the small details about the language as I would like. I've been digging through my reference book but the answer to this one eludes me.

Now, I had a C++ course a few years ago, and therein we emphasized that in C++ everything is types, so I try to keep that in mind when thinking things through. Deconstructing the expression: "static_cast<MyType>(0);", it indeed seems to me that we take a literal zero, cast it to a pointer to MyType (which makes it a null pointer), and then apply the dereferencing operator in the context of assigning to a reference type (the return type), which should give me a reference to the same object pointed to by the pointer. This sure looks like returning a null reference to me.

Any advice in explaining why this works (or why it shouldn't) would be greatly appreciated.

Thanks, Chuck

like image 763
Chuck Avatar asked May 24 '10 05:05

Chuck


People also ask

What is return null in C?

NULL can be used if a function returns a pointer. In this case, you return an object, which means that you have to return a real, existing object. One way of doing this is to have an "ok" field in the struct that you could set in the init function, and that you could check in the caller.

Can you return a null pointer in C?

As we know, that malloc() function allocates the memory; if malloc() function is not able to allocate the memory, then it returns the NULL pointer.

WHAT IS null dereference in C?

A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit. Extended Description. NULL pointer dereference issues can occur through a number of flaws, including race conditions, and simple programming omissions.


1 Answers

That is undefined behavior. Because it is undefined behavior, it may "work" on your current compiler, but it could break if you ever upgrade/change your compiler.

From the C++03 spec:

8.3.2/4 ... 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.

like image 120
R Samuel Klatchko Avatar answered Oct 19 '22 18:10

R Samuel Klatchko