check pointer by "!" - page 3

 
Dominik Egert #:
Played long enough, the point is, no matter what you do, both will evaluate to true, because in both cases you want to enter the if-statement.

!ptr if invalid will turn true.
!NULL will be true.
CheckPointer(ptr) == POINTER_INVALID will be true.

It doesn't matter. The only difference to ptr == NULL is, they are "function calls" or better said more than a comparison.

And no matter if ptr = NULL, it will still be a function call if you do: !ptr.

Get it now? If so, go back and check my answers, I gave and see if they now make sense.

Ok, thank you! According to documentation !ptr will also do a check to CheckPointer(ptr) == POINTER_INVALID.

To sum up:

Is this

if(pointerObj == NULL || CheckPointer(pointerObj)==POINTER_INVALID){..}

the same as this:

if(!pointerObj){..}

The answer is yes. So the MQL5 compiler handles NULL in a special way, e.g.: !NULL (regardless of whether it is a pointer or object) returns true.

 
Dominik Egert #:
!NULL => true

NULL is void


 
Vladislav Boyko #:

NULL is void


Yes. It was examplatory.
 
harryma23 #:

Ok, thank you! According to documentation !ptr will also do a check to CheckPointer(ptr) == POINTER_INVALID.

To sum up:

Is this

the same as this:

The answer is yes. So the MQL5 compiler handles NULL in a special way, e.g.: !NULL (regardless of whether it is a pointer or object) returns true.


The answer is not yes, it depends what you are asking.

Is the outcome the same, yes. Is the method the same, no.

One is a branch, the other isn't. That is, its not the same. That's what I said initially, one is a short-circuit, the other isn't. But the outcome is the same.

And thank you for actually turning on your gray matter and following along.
 
Vladislav Boyko #:
NULL is void

This makes more sense. So at the end:

This

f(pointerObj == NULL || !pointerObj){..}

is the same as 

if(pointerObj == NULL || CheckPointer(pointerObj)==POINTER_INVALID){..}

I don't know if the compiler treats void in CheckPointer in any special way. I don't think so. 

 
harryma23 #:

This makes more sense. So at the end:

This

is the same as 

I don't know if the compiler treats void in CheckPointer in any special way. I don't think so. 

It doesn't.

The shortest and fastest check is !ptr.

Edit: it is reliable enough for all cases.
 
What patterns make you need to check pointers? I don't check pointers because I know whether a particular pointer is valid at the coding stage.
 
Vladislav Boyko #:
What patterns make you need to check pointers? I don't check pointers because I know whether a particular pointer is valid at the coding stage.
My first reply.
 
harryma23 #:

This makes more sense. So at the end:

This

is the same as 

I don't know if the compiler treats void in CheckPointer in any special way. I don't think so. 


A NULL pointer, by definition, is invalid. Always.
 
Vladislav Boyko #:
I don't check pointers because I know whether a particular pointer is valid at the coding stage.

Here's an example (the first one I came across; it's MQL4, sorry) where I avoid checking the pointer using an out bool variable

class CRatesMgr
  {
public:
   const CRates* chartFind(bool &found, ENUM_TIMEFRAMES tf) const
     {
      if(tf == PERIOD_CURRENT)
         tf = (ENUM_TIMEFRAMES)Period();
      int idx = CRatesArrBuilder::arrIdxByTf(charts, tf);
      if(idx == -1)
        {
         found = false;
         return NULL;
        }
      found = true;
      return charts[idx];
     }
   
private:
   CRates* charts[];
  };