News for hackers. Stuff that matters.
Re:Null Pointer Dereferencing Causes Undefined Behavior by nkurz on Tuesday April 21, 2015 @04:41AM (#9412850)
I like your detailed example, but wouldn't a slightly kinder but equally optimizing compiler simply add a conditional before the load:

  if (max > 0) {
    int i = max; 
    int tmp = p[0].foo;
    do {
       bar += tmp;
    } while (--i);
  }
I'd even argue the assembly is slightly better when interpreted this way, since branch prediction will tend execute the load or not as appropriate, rather than potentially taking a unnecessary TLB miss for a load that's never used.

There are no comments on this comment yet.