check pointer by "!" - page 4

 
Dominik Egert #:
My first reply.
You probably misunderstood me. I meant, "What OOP patterns make you need to check pointers?"
 
Dominik Egert #:
It doesn't.

The shortest and fastest check is !ptr.

Edit: it is reliable enough for all cases.

I never use "!ptr" because I don't know what it does. Never trust the documentation 😉

I either is ptr==NULL if I am sure !NULL is valid, or the full if version as shown in the topic.

 
Vladislav Boyko #:
You probably misunderstood me. I meant, "What OOP patterns make you need to check pointers?"

I find the question strange. An OOP pattern is not about implementation details, so not about using pointers or not, so not about checking them or not.

Unless I missed something ?

 
Dominik Egert #:

A NULL pointer, by definition, is invalid. Always.

Yes, thus I need to check beforehand to avoid null pointer exceptions. That's what this topic is about. The only crux of this whole topic is:

Using !(pointerObj) handles both NULL and INVALID pointers.

If you look at many places in the MQL5 source code, you'll see that only (objectPtr != null) is checked to avoid NPEs.. So I was'not entirely sure why it's not checked with !I(pointerObj).

 
Alain Verleyen #:
I either is ptr==NULL if I am sure !NULL is valid, or the full if version as shown in the topic.

I think CheckPointer() is better:

class A {};

#define _print(x) Print(#x" = ", x)

void OnStart()
  {
   A* a = NULL;
   f(a);
   _print(a == NULL);
   _print(CheckPointer(a) == POINTER_INVALID);
  }

void f(A*& a)
  {
   a = new A();
   delete a;
  }


 
Alain Verleyen #:

I find the question strange. An OOP pattern is not about implementation details, so not about using pointers or not, so not about checking them or not.

Unless I missed something ?

I’ve noticed that I never really need to check a pointer for validity. In situations where such a check might seem appropriate, I manage without it. That made me wonder: in what cases do you find it necessary to check pointer validity? I thought maybe you use some advanced OOP patterns where this becomes unavoidable.
 
harryma23 #:
Using !(pointerObj) handles both NULL and INVALID pointers.

You're wrong, here's the proof. You still haven't listened to the best advice.

No, I'm wrong. I forgot that !pointer is an implicit call to CheckPointer(). But I would still prefer an explicit call.

 
Vladislav Boyko #:

You're wrong, here's the proof. You still haven't listened to the best advice.

No, I'm wrong. I forgot that !pointer is an implicit call to CheckPointer(). But I would still prefer an explicit call.

My first post on this thread.


No, I dont want to be annoying.
 
Vladislav Boyko #:

I think CheckPointer() is better:


I wasn't clear enough previously.

Either I know the pointer can only be valid or NULL. Then I will just use : if(ptr!=NULL) ...

if(pointerObj!=NULL) { do my stuff }...

But sometimes the pointer can be different of NULL and be invalid (as you have showed), then I will use the "full" 

if(pointerObj!=NULL && CheckPointer(pointerObj)!=POINTER_INVALID) { do my stuff } ...

I don't like to use because it's unclear and I don't know what is really done (I don't trust the documentation).

if(!pointerObj)
 
Alain Verleyen #:

I wasn't clear enough previously.

Either I know the pointer can only be valid or NULL. Then I will just use : if(ptr!=NULL) ...

But sometimes the pointer can be different of NULL and be invalid (as you have showed), then I will use the "full" 

I don't like to use because it's unclear and I don't know what is really done (I don't trust the documentation).

Yes, I think so too. That's the only clear and direct answer on the entire topic. Thanks.