Well, you dont need to check the pointer against NULL if you are using CheckPointer().
I thought about that too at first, but then I realized that when (pointerObj!=NULL), CheckPointer() won't be called. It seems like a unified check for all cases.
Vladislav Boyko #: I thought about that too at first, but then I realized that when (pointerObj!=NULL), CheckPointer() won't be called. It seems like a unified check for all cases.
I am very careful about branches, as they are extremely expensive.
If you unequivocally call CheckPointer its a non branching code, and can mostly be fully inlined, therefore I try to stick to only one approach.
But probably its irrelevant for most use cases.
This is branching:
if(pointerObj!=NULL && CheckPointer(pointerObj)!=POINTER_INVALID) { do my stuff } ...
Vladislav Boyko #: I thought about that too at first, but then I realized that when (pointerObj!=NULL), CheckPointer() won't be called. It seems like a unified check for all cases.
Not sure if i understood correctly, but the check against != NULL, followed by an && will end in a short circuit, if first evaluation (to the left of &&) is false. - Its the other way around for || because the result is already defined at that point.
When running code, it is often better to do the work in any case, instead of selective processing. This has to do with the pipelining of CPUs.
Clearing out the pipeline due to a faulty branch prediction is very expensive.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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).
I thought about that too at first, but then I realized that when (pointerObj!=NULL), CheckPointer() won't be called. It seems like a unified check for all cases.
So you are suggesting to not use the if shortcut ?
I understand the concept behind what you said but I can hardly believe it's useful in practice (about if shortcut like here).
Though I would be happy to learn I am wrong. Are you able to demonstrate the impact ? (not necessarily with pointer==NULL, I don't care).
"do my stuff" usually means pointer accesses, so you have branching either way.
I thought about that too at first, but then I realized that when (pointerObj!=NULL), CheckPointer() won't be called. It seems like a unified check for all cases.
"do my stuff" usually means pointer accesses, so you have branching either way.
Its the other way around for || because the result is already defined at that point.
Yes, || looks more attractive
Yes, || looks more attractive