Link pointing to a Null object

I saw this discussion - Checking a null object in C ++ , and I was surprised that no one was talking about when a link could point to a null object. In our code we usually use null objects. The following functions return nullObj.

const Obj& nullObj() { static obj* nullPtr = NULL; return static_cast< const Obj&>(*nullPtr); } 

Actually, when I looked at the code again to bring up this question, I had some questions about how this code works:

  • How to do it *nullPtr - This is because nullPtr is a static object that allocates memory on the heap and therefore is guaranteed to have some space and

  • Since we are returning a const reference for obj, does the compiler create a temporary object (for some type nullObj ??) or will the const reference be referenced as an alias for nullPtr?

+6
source share
3 answers

I was surprised that no one talked about when a link could point to a null object

This is because he cannot, in the right program.

Your function, which has a nullpointer difference, has Undefined Behavior . The compiler is allowed to emit code that, for example, causes a failure at this point.

However, one of the possible effects of UB is that the code does what he thought it would do. Thus, null references may occur. I have never come across this, but if so, it means that there is a serious logical error in the code.

All the functions of the null reference object that you use that you show are logical errors.

It’s best to use these methods and fix things .; -)

Cheers and hth.,

+17
source

I was surprised that no one said when a link could point to a null object.

Never.

(i) How can this be done * nullPtr - this is because nullPtr is a static object that is allocated memory on the heap, and therefore it has space and address allocated for deref?

This is not true. You are looking for a null pointer that causes undefined behavior.

(ii) Since we are returning a const reference for obj, does the compiler create a temporary object (for some type nullObj ??) or will the const reference be referenced as an alias for nullPtr?

Not. At this point, the compiler is allowed to create nasal demons or a black hole. If you are lucky, you will get a segmentation error or some other access to the violation.

DO NOT DO IT

+5
source

(i) How can this be done * nullPtr - Is it because nullPtr is a static object that allocates heap memory and, therefore, has a specific space and address for deref?

No, because nullPtr is a pointer initialized to NULL, it is not an object at all.

(ii) Since we are returning a const reference to obj, does the compiler create a temporary object (for some nullObj ??) or will the const link act as an alias for nullPtr itself?

It will be an alias for an object located at a NULL memory address. Ie: When you really refer to any of the members of this reference object, you will get anything (usually an access violation if the system is smart enough, but it could be anything, the behavior is undefind).

+1
source

Source: https://habr.com/ru/post/890980/


All Articles