Page 1 of 1

Note-to-self: short circuiting==buggy programs

Posted: Wed Jan 28, 2009 1:04 pm
by earlz
Ok, well, recently I learned in my Java programming class about short circuiting.. like
if(foo() && bar()) if foo is false, then bar will never be executed because the condition is automatically false.

Well, I wondered if C did this by default or different optimization levels and such..
GCC did this by default no matter what the optimization level, as did PCC(bsd C compiler).

Now, I'm just giving everyone a warning as to this behavior. I am almost for sure somewhere in my old code of millions of bugs, I was unaware of this.

This will probably not usually be a problem, as I can't even think of a decent example of a bug made by this.
but here is my shot at it..

Code: Select all

//shutdown code
//functions return 1 on error.
if(shutdown_terminals() || sync_disks()){
kprintf("An error occured shutting down, attempting to continue\n");
}
//....
in this example, if say some terminal got locked and could not be shutdown or something, it would return an error. This would mean the compiler would short-circuit it. So shutdown_terminals would be executed and it would return 1. This would make the OR condition TRUE. So to the compiler, there is no need to try to sync disks because the condition is already true.


Just saying all this to maybe aid someone out there trying to find a hard bug.

Re: Note-to-self: short circuiting==buggy programs

Posted: Wed Jan 28, 2009 1:58 pm
by stephenj
If you haven't already, you may want to read the K&R C book. There are a few things like this you may want to know.

Also, short-circuiting can be used to prevent errors (short circuiting != buggy programs) without writing a lot of code. Take a look at this (simple) example:

Code: Select all

while (NULL != x && 'a' != x->val) {
	x = x->next;
}
Consider the alternative without short-circuiting. We'd have to do the NULL test before the loop, and then just before it iterates. If you don't do the NULL test, then iff NULL == x, you should segfault asking for x->val.

In any event, misusing any tool may cause problems. This shouldn't really surprise anyone.

(BTW, if you look at my test expression within the while loop, you may notice that I put the constant before the variable. This prevents the annoying x = NULL issue when you meant x == NULL.)

Re: Note-to-self: short circuiting==buggy programs

Posted: Wed Jan 28, 2009 2:23 pm
by earlz
hmm... I guess that bit is pretty nifty... maybe there should be a way to toggle short circuiting :P lol

Re: Note-to-self: short circuiting==buggy programs

Posted: Wed Jan 28, 2009 2:33 pm
by stephenj
There is, you use | instead of ||, and & instead of &&.

Re: Note-to-self: short circuiting==buggy programs

Posted: Wed Jan 28, 2009 2:59 pm
by earlz
hmm... wasn't aware of that... thanks

Re: Note-to-self: short circuiting==buggy programs

Posted: Wed Jan 28, 2009 8:38 pm
by iammisc
Java got the idea of "short-circuiting" from C.

Re: Note-to-self: short circuiting==buggy programs

Posted: Thu Jan 29, 2009 1:12 am
by Solar
Short-circuiting shouldn't come as a surprise; it's part of the language, every text I've seen so far on && and || mentions this, and as stevenj pointed out, it is used in many "standard" constructs (precondition checking).

Re: Note-to-self: short circuiting==buggy programs

Posted: Fri Jan 30, 2009 5:00 am
by B.E
One of the advantages of short circuiting is this if you want to stop processing after an error and you have a few functions to call. It becomes easy to do.

For example, without short circuiting:

Code: Select all

if (!b=function1(a){
   printf("Failed to preform operation");
}else if (!c=function2(b){
   printf("Failed to preform operation");
}else if (!function3(c){
   printf("Failed to preform operation");
}
...
Would become:

Code: Select all

if (!(b=function1(a) && c=function2(b) && function3(c)))
   printf("Failed to preform operation");
Another example:

Code: Select all

if (hasNext(b) && n = Next(B)){
 // do something
} else{
 // has no next 
}