"It doesn't make any sense!", the readers will argue. Why, it "does": this code "allows" you to call the GetSafeHandle() method through a null CWindow* pointer. This method is sometimes used in different projects. Let's find out why doing so is really a bad idea.

First of all according to the C++ standard (it follows from the paragraph 5.2.5/3 of the standard ISO/IEC 14882:2003(E)), calling any nonstatic method of any class through a null-pointer leads to undefined behavior. However, a code shown below may work in certain implementations:

It can work because no attempts are made to access the class members while the method is executed, and no late binding is used to call the method. The compiler knows which particular method of which particular class should be called and simply adds the necessary call. "This" pointer is passed as a parameter. The effect produced is the same as if it were a static method:

If the method were called virtually, it would have required late binding which is usually implemented through a pointer to the virtual methods table in the beginning of a particular object. In this case even finding out which method to call would require accessing the object's content, and this operation would most likely cause a crash in case of a null-pointer.

But we know for sure that our method will never be called virtually, don't we? After all, this code has been working well for some years.

The trouble is that the compiler may utilize undefined behavior for the purpose of optimization. For example:

In this code fragment we have an integer division by divideBy. Integer division by null causes undefined behavior (usually a crash). Therefore we can assume that the divideBy variable is not equal to null and eliminate the check during compilation and optimize the code accordingly.

In the same way the compiler can optimize the code comparing "this" pointer to null. According to the Standard, this cannot be null and therefore the checks and the corresponding code branches can be eliminated, which will greatly affect the code dependent on the comparison of "this" pointer to null. The compiler has a full right to "break" (actually just break it further) the code CWindow::GetSafeHandle() and generate machine code which doesn't contain the comparison and only reads the class field all the time.

Currently even the freshest versions of the most popular compilers (you can check it with at the GCC Explorer service) don't perform such optimizations, so "everything works" for now, right?

First, you will be very disappointed to waste quite a lot of time to find out that there is such an optimization now, after having moved to another compiler or a new version of your current compiler. That's why the code shown above is non-portable.

GOOD LORD, the "this" pointer equals 0x00000004 on entering the method when compiled in Visual C++ 9, as the pointer initially set to null is adjusted so that it points to the beginning of a subobject of the corresponding class.

this will become null at the same conditions because the beginning of the subobject coincides with the beginning of the object it is included into. Thus we get a wonderful class whose method works only if this class is used "appropriately" in compound objects. I wish you good luck with debugging; the Darwin award has seldom been so close.

One can easily notice that implicit conversion of the pointer to the object to a pointer to the subobjectworks "wrong" in the case of the Composed1 class: this conversion yields a non-null pointer to the subobject from a null-pointer. When implementing a conversion of the same meaning, the compiler usually adds a check of the pointer for being null. For example, compilation of the following code with undefined behavior (the Composed1 class is the same as shown above):

The second instruction in this machine code is a comparison of the pointer to the object to null. If the check reveals that the pointer is equal to null, the control is not allowed to pass through the add eax,4 instruction which is used to shift the pointer. An implicit conversion here is implemented with a check, though it was also possible to call the method through the pointer and assume that the pointer is non-null.

In the first case (calling the subobject's class method straight through the pointer to the class object), the fact of the pointer being equal to null also corresponds to undefined behavior, and the check is not added here. If you thought it to be rubbish and fantasy when reading the paragraph about optimization of a code with a method call and pointer check for null after it, you shouldn't: the case described above is just the one where such an optimization has actually been used.

It's a bad idea to rely on calling a nonstatic method through a null-pointer. If you want to enable a method to be executed for a null pointer, you should make the method static and explicitly pass the pointer to the object as a parameter.

Use PVS-Studio to search for bugs in C, C++, C# and Java

We offer you to check your project code with PVS-Studio. Just one bug found in the project will show you the benefits of the static code analysis methodology better than a dozen of the articles.