Still Comparing "this" Pointer to Null?--Dmitry Meshcheryakov

The article explains why one must never use the (this == 0) expression. It's a very bad code. It being able to work at times doesn't mean anything. By writing (this == 0), you let undefined behavior into your program.

Still Comparing "this" Pointer to Null?

by Dmitry Meshcheryakov

From the article:

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.
...
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.

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.