Was the class function compiled with if (this == NULL)?

I saw this piece of code during our lab and actually compiles in MSVC2008 and g ++.

void LinkList< class T >::Insert(T n) { if (this == NULL) // some code here } 

As far as I know, this should not be empty, since you cannot call class functions in C ++ if it has not been created. Is this valid code? if so, why and where can it be useful?

+6
c ++
source share
5 answers

since you cannot call class functions in C ++ if it was not created

The fact is that you can, but leads to undefined behavior .

Such a check should probably be a statement, although such code is not guaranteed to actually work by standard. (If this is NULL, you are already in the undefined range.)


The reason for "useful" is the discovery of an object after its removal or if it has never been created:

 template <typename T> // I hate this function void safe_delete(T*& pPtr) { delete pPtr; pPtr = 0; } T* p = new T; safe_delete(p); p->foo(); // this is null, and we've entered undefined behavior 

Inside foo you can say: "hey, we messed up: /".

In my opinion, such use indicates poor design. You should not have a pointer lying around that can be called again. The last thing you do with the pointer is delete it; if it is still around, change your code so that it is not.

+8
source share

This is the type of problem he is trying to “solve”:

 class A { public: void test() { if(this == NULL) { std::cout<<"This is NULL\n"; } } }; int main() { A* p = NULL; p->test(); return 0; } 

However, note that calling p->test() causes undefined behavior, so this may or may not be NULL. I assume that someone was in a hurry and decided to do this instead of solving the real problem. Do not use it; it will never work reliably or predictably. As for why it compiles, this is just a const pointer and comparing a pointer to NULL is syntactically correct.

+5
source share

if the class variable is not created ... The behavior is undefined ... Therefore, the compiler will not give you an error ... But the behavior of the function is undefined ... It can even work fine if no member of the class is used before the expression if (this == NULL) ...

0
source share

Since this is just a pointer, the code is valid - this means the comparison is valid. Regardless of whether the code is executed in the conditional block or not, this is another problem.

0
source share

According to the C ++ standard, this behavior is undefined. However, a typical C ++ implementation will consider a non-virtual member function

 void A::func(int arg); 

as a function of C-style

 void A_func(A* this, int arg); 

And just like any pointer argument, there must be NULL for "this" if you are not looking for memory. So if you write:

 #include <iostream> class A { public: void test(); }; void A::test() { std::cout << "This function could have been static." << std::endl; } int main() { ((A*) NULL)->test(); return 0; } 

It will work very well (at least under MSVC and g ++).

However, this gives a segmentation error:

 #include <iostream> class A { public: void test(); private: int _n; }; void A::test() { _n = 42; } int main() { ((A*) NULL)->test(); return 0; } 

Check if (this == NULL) - an attempt to prevent dereferencing of a null pointer when a program crashes.

But to comply with the standard, it is better to do the test outside.

 if (pList != NULL) { pList->Insert(value); } 
0
source share

All Articles