Gamestudio Links
Zorro Links
Newest Posts
WFO Training with parallel cores Zorro64
by Martin_HH. 02/24/26 19:51
Zorro version 3.0 prerelease!
by TipmyPip. 02/24/26 17:09
ZorroGPT
by TipmyPip. 02/23/26 21:52
Camera always moves upwards?
by clonman. 02/21/26 09:29
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 02/19/26 13:22
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
5 registered members (Martin_HH, TipmyPip, AndrewAMD, Grant, USER0328), 5,287 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
alx, ApprenticeInMuc, PatrickH90, USER0328, Sfrdragon
19199 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Optimised comparisons #285399
08/18/09 11:49
08/18/09 11:49
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
I'm pretty sure that in standard C, && and || comparisons are optimised so that, in the case of &&, if the first comparison returns false the whole thing returns false without checking the second comparison. Similarly, in the case of ||, if the first comparison returns true the whole thing returns true without checking the second comparison.

Maybe it's not standard, but I've programmed like that before. How do I know? See this example:
Code:
ENTITY* closest = NULL;
if (closest == NULL || closest.skill[0] == 5)
	closest = NULL;

While the contents of the if expression aren't useful in this situation, this is a simplified example. This gets a crash, and I think it's because it's looking for "closest.skill[0]" even when "closest == NULL", but in an ideal world (as well as programming in C, C++ or Java, if I recall correctly) that would never happen because if closest == NULL then the whole if-expression would already return TRUE.

Would it be possible in future for an "early bail" if the whole expression is already known to be true (or in the case of &&, false)?

Workarounds for this are ugly (in my actual situation I have a relatively large chunk of code in the "if" case, and my only alternatives seem to be having identical chunks for two separate cases (closest == NULL... else if closest.skill[1]...), or using an if...else if... to set a variable which is then used in the final comparison instead). Unless I've done something really stupid here laugh

Jibb


Formerly known as JulzMighty.
I made KarBOOM!
Re: Optimised comparisons [Re: JibbSmart] #285406
08/18/09 12:38
08/18/09 12:38
Joined: Jul 2005
Posts: 1,930
Austria
Dark_samurai Offline
Serious User
Dark_samurai  Offline
Serious User

Joined: Jul 2005
Posts: 1,930
Austria
I also often run into this problem! Solving this would be very useful!


ANet - A stable and secure network plugin with multi-zone, unlimited players, voip, server-list features,... (for A7/A8)!
get free version
Re: Optimised comparisons [Re: JibbSmart] #285408
08/18/09 12:40
08/18/09 12:40
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
There was a thread about this a while ago: look here

It was said that this will be fixed in the future.

Re: Optimised comparisons [Re: Saturnus] #285410
08/18/09 12:45
08/18/09 12:45
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
Thanks for the heads-up Kombucha laugh Sorry I missed it -- didn't think it would be in Bug-Hunt.

Jibb


Formerly known as JulzMighty.
I made KarBOOM!
Re: Optimised comparisons [Re: JibbSmart] #285415
08/18/09 12:53
08/18/09 12:53
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
Nah, no need for an excuse. It's really hard to find, if you don't know it's there. : )

Re: Optimised comparisons [Re: Saturnus] #285458
08/18/09 16:09
08/18/09 16:09
Joined: Jul 2000
Posts: 28,075
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,075
Frankfurt
Anyway, this is a justified request and it's on my list.

Re: Optimised comparisons [Re: jcl] #285560
08/19/09 01:53
08/19/09 01:53
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline OP
Expert
JibbSmart  Offline OP
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
Thanks jcl!

Jibb


Formerly known as JulzMighty.
I made KarBOOM!

Moderated by  aztec, Spirit 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1