tags:

views:

185

answers:

4
+10  A: 

In both cases you are hitting undefined behavior. Anything can happen. In each post-fix operator the compiler must retrieve the value of the variable before performing the operation, but the order of execution of that operation with respect to the other post-fix operations in the code is undefined.

For the simpler expression int c = a++ + a--; all these are valid execution paths according to the standard:

// assume a=10
// option 1
int c = a + a;   // 20
a++; a--;
// option 2
register int tmp = a; // the register keyword is just to show that no real memory is created
a++;
int c = tmp + a; // 21
a--;
// option 3
register int tmp = a;
a--;
int c = tmp + a; // 19 
a++;
David Rodríguez - dribeas
That doesn't explain why this particular behavior happened.
SLaks
When I ran these programs they formatted my hard drive and then my speakers started playing vuvuzela sounds. Goddamned undefined behavior. :(
Tyler McHenry
@Tyler: +1 for the vuvuzela
Petar Minchev
@SLaks, it's implementation specific undefined behavior :)
Nick D
@Slaks: Actually, it does. In C, there are certain things in the spec that are declared as being undefined behavior... each compiler may treat it differently. Using post-increment and post-decrement on the same variable in the same expression is one of them.
R. Bemrose
@SLaks: Explaining any particular result of _undefined behaviour_ has little value. If there is an explanation - which is usually implementation simplicity at a level that has little or no visibility to the compiler user - it may change between compiler versions, platforms or even day of the week.
Charles Bailey
@SLacks: I have added three different possible and valid outputs for a simpler expression that shows different code paths that are valid and produce different outputs. The problem there is the program: you cannot depend on undefined behavior. Slight differences in code, or compiler/optimizer options might yield different results. Even the same exact compiler options and code can yield different results (some compilers limit the amount of time they let the optimizer run, so depending on the hardware or even the system load the output can differ)
David Rodríguez - dribeas
@Charles Bailey: Actually, UB of a single code snippet can do two different things when used twice in the same program (depending on decisions of the compiler, number of registers currently available etc.). UB means the program may do anything, and need not be consistent with anything, even itself.
jpalecek
@jpalecek: I didn't mean to even hint otherwise.
Charles Bailey
+2  A: 

The b++ + b-- attempts to modify b twice without an intervening sequence point. This leads to undefined behavior.

Jerry Coffin
+1  A: 

To explain the second case, I assume that the following values are used:

c = 2 + 3 + 3 + 4 = 12

In the first case I can only assume that a and b are decremented by the -- before being added giving:

c = 2 + 2 + 3 + 3 = 10
ChrisF
A: 

Not on my box.

#include <stdio.h>
void func1(){
int a=2,b=3,c;
c = a++ + a-- + b++ + b--;
printf("func1: %d\n", c);
}

void func2(){
int a=2,b=3;
int c = a++ + a-- + b++ + b--;
printf("func2: %d\n", c);
}


  int main(){
func1();
func2();
return 0;
}

When I compiled and ran it I got "func1: 10" and "func2: 10". I'd try a debugger to see what is going on with your system. In case it matters, I compiled with gcc on a mac.

Terry
I think that the first code sample is c and the second c++. So the question is why compiling the same code under different compilers gives different results.
ChrisF
but when i compiled it on turbo c++ compiler it generate both different ans 10 and 12 respectevely
picnic4u