Author: Dann Corbit
Date: 15:43:53 01/18/06
Go up one level in this thread
On January 18, 2006 at 17:56:08, Robert Hyatt wrote: >On January 17, 2006 at 17:50:33, Dan Honeycutt wrote: > >>On January 17, 2006 at 14:59:15, Robert Hyatt wrote: >> >>... >>> >>>if (a && b) where if a is 0, b would not even be fetched usually... >>> >> >>I thought, in this case, b _must_ not be fetched. >> >>Best >>Dan H. > > >I don't think so. Compiler optimizers are good at "lifting" instructions back >up the instruction stream so that memory reads can be started well before the >data is needed. As a result, on a Cray for example, it is likely both of those >loads would be done at the same point way back up in the code (the machine has >enough registers to make this work well) so that when we get to this point in >the code, the two comparisons can be done instantly. > >Perhaps you are thinking that the two conditional tests must be done >left-to-right, and so far as I know that is correct. But fetching the operands >left to right I am not sure about. I am sure that there have been compilers in >the past that did what I described. Personally I'd consider it a bad >programming practice to write something like that (for example, if ((i < n) && >(a[i] == 0)) where I could have a value > n, which would be outside the >subscript range for the a array). For the logical && operator, the "AS IF" rule is applied. So the compiler is allowed to do whatever it wants, but it MUST evaluate "AS IF" the shortcut rule were applied. So: if (a || b) foo(); SHALL NOT evaluate b if a is non-zero. if (a && b) foo(); SHALL NOT evaluate b if z is zero. So the program is not allowed to do a core dump, even if b is an uninitialized pointer or a trap representation in either case. That does not mean that it WON'T. But if it does, you can complain to your compiler vendor about their broken implementation. If they advertize an ANSI or ISO compiler, they will also be liable to fix it. This would be true even in a non-hosted implementation (e.g. a compiler for your toaster's IC).
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.