Linked Questions
13 questions linked to/from Does it ever make sense to check if "this" is null?
0 votes
1 answer
537 views
Checking whether (this == nullptr) in a class method [duplicate]
I would like to replace code like this: if ((obj != nullptr) && obj->is_valid()) with if (obj->is_valid()) where class Obj { bool is_valid() { if (this == nullptr) ...
1 vote
2 answers
223 views
In a member function, can this == NULL? [duplicate]
Lets say I have the following class struct FooList { int data; FooList *next; // basic constructor FooList(int d, FooList *n): data(d), next(n) {} // basic destructor ~...
3 votes
0 answers
322 views
Checking "this" for nullptr - undefined behaviour? [duplicate]
Let's consider this piece of code: struct config { int get( ) { if ( this == nullptr ) return 1; return value; } int value = 5; }; config* c = nullptr; int result = ...
0 votes
0 answers
136 views
How Can this == nullptr Ever be true Without Invoking Undefined Behavior? [duplicate]
I was recently reading Programming: Principles and Practice Using C++ (2nd Edition), and I came upon this snippet inside a function for inserting into a linked list (Page 619): if (n==nullptr) return ...
0 votes
0 answers
77 views
In C++, is checking `this == nullptr` inside member functions an acceptable pattern? [duplicate]
I have seen C++ classes that check the this pointer to make sure it is not nullptr before using a field. Here is a simplified example of what I'm talking about: #include <iostream> class object ...
120 votes
11 answers
30k views
Can "this" ever be null in Java?
Saw this line in a class method and my first reaction was to ridicule the developer that wrote it.. But then, I figured I should make sure I was right first. public void dataViewActivated(...
150 votes
5 answers
10k views
Why does the enhanced GCC 6 optimizer break practical C++ code?
GCC 6 has a new optimizer feature: It assumes that this is always not null and optimizes based on that. Value range propagation now assumes that the this pointer of C++ member functions is non-null. ...
76 votes
11 answers
7k views
How bad is "if (!this)" in a C++ member function?
If I come across old code that does if (!this) return; in an app, how severe a risk is this? Is it a dangerous ticking time bomb that requires an immediate app-wide search and destroy effort, or is it ...
0 votes
2 answers
2k views
Qt test current object/widget existence
In Qt I have to wait for 1 second and let event process so I use QTime lDieTime= QTime::currentTime().addSecs(1); while (QTime::currentTime() < lDieTime) QCoreApplication::processEvents(...
1 vote
3 answers
157 views
Method in null class pointer (c++)
Let's say whe have class Foo{ public: bool error; ...... bool isValid(){return error==false;} }; and somewhere Foo *aFoo=NULL; I usually would do if (aFoo!=NULL && ...
1 vote
2 answers
2k views
How to call native method with 'null' parameter
I need call native method like that: java code: private native Object SendFiles(String strEmail, DeviceInfo info, String [] arrFiles, int cntFiles, ID idRequest); call SendFiles(); SendFiles(...
0 votes
1 answer
671 views
Checking whether current object is destroyed or not inside the same class in C++
I have to call a function of a class from another function of the same class. But the problem is for some cases the current object can be destroyed. So for this case calling the function is creating ...
1 vote
1 answer
109 views
Why is this test not tautological?
This is not about explicit code, so apologies if it should have been posted elsewhere. It is, however, solidly within the domain of testing, which I assume you guys to be right at home in. I'm ...