C or C++? Opinions.

Started by
48 comments, last by RageMatrix 22 years, 9 months ago
Sodding post decrement operator weirdness... arg....

Still, it answers the question... They are both the same
Advertisement
Sorry, I didn''t realize the compiler used extended registers when comparing...we will need to use a neutral language to evaluate this :-)

__asm
{
mov al, 0FFh
mov dl, al
inc dl
cmp al, dl
ja C_IS_BETTER

;;;;c++ is better;;;;

C_IS_BETTER:
;;;;c is better;;;;
}

That should do it right

--Andrew
lol
But then you are not really comparing c++ to c.
int c = 456;
if(c > c++) printf("Blah.");

You''ll find that this does display that message. I''ll explain why.

c++ is evalutated as 456, then it adds 1 to c. But now, c is equal to 457. Since 457 is greater then 456, it displays the message. I have actually tested this. And it will continue to work as long as ++ operators are evaluated before the rest of the expression.

Try using a preincrement operator and watch as (c == ++c).
I believe that this relies on undefined behaviour, and can''t be relied on across all platforms and all compilers.

In fact, my gut feeling is that it is simply incorrect behaviour. The post-increment should almost certainly come after the evaluation of the condition, which is the whole reason for the existence of the post-increment operator to complement the pre-increment one. The ''condition'' in this case is (c > c), which should always be false, given a standard type such as ''int''. And the post-increment should come after the condition is tested, so the message should never be seen.
If that is printing anything for you then you have a funky compiler. I''m pretty sure it is not even undefined behavior. I will have to look it up in the specification but I''m pretty sure that increment/decrement operations are defined as taking place after the evaluation. In function calls,

test( c++ );

will send c to the function and increment when the function returns.

test( ++c );
will increment and send the new value of c to the function

But boolean evaluations are handled differently in that all increment or decrement operations inside of the () are performed after the evaluation. (c == c++) and (c == ++c) and (c++ == c) and (++c == c).

Seeya
Krippy
Beer hunter: what compiler are you using?

I found that even enclosing the c++ in brackets doesnt help. Nasty.



Post- and pre-increment are VERY dangerous to use in a situation where the variable appears more than once in the statement. This is because the behaviour within a single statement is undefined!

The statement c > c++ is dangerous for the following reason:

the value of c++ is c, that''s the definition of post-increment.
However, the value of c after c++ is c + 1.
Now, the problem is, c here appears before c++, so in truth, it is unsure whether the increment of c should happen WITHIN the statement, or AFTER the statement. If it occurs immediately, then c > c++ is indeed true (!!).

It depends on compiler implementation as well, but I could see the ++ operator being defined as follows:

int operator++()
{
this = this + 1;
return this-1;
}

In fact, from Stroustrup''s book on C++:
"y = x++ is equivalent to y = (t = x, x += 1, t )"
That means in the above case:
c > ( c = ( t = c; c += 1; t ) )

So then it boils down to evaluation order - normally, the first c would be evaluated first because of the left-to-right order, and c > c++ should be false.



People might not remember what you said, or what you did, but they will always remember how you made them feel.
Mad Keith the V.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
For hilarity''s sake, I tried the following in VC++:
int c = 0;
printf( "%i %i %i\n", c, c++, c );
printf( "%i\n", c );

int d = 1;
printf( "%i %i %i\n", d, ++d, d );
printf( "%i\n", d );

Output:

0 0 0
1
2 2 1 (!!!!!!)
2


Obviously, VC++ evaluates right-to-left, and uses different rules for the post- and pre-increment operators.

The basic moral of the story:
just DON''T DO IT!


People might not remember what you said, or what you did, but they will always remember how you made them feel.
Mad Keith the V.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Ok.
In functions, multiple arguments are evaluated right to left. post-increment is done after the function call, pre-increment is done right before pushing the argument in question to the stack. But in evaluations it is still different.


Consider this:
            int c = 1;if ( (c++ == c++) && (c++ == c++) && (++c == ++c) && ( ++c == ++c ) && (c++ == c++) && (c++ == c++) && (++c == ++c) && (++c == ++c) && ( c == 17 ) && (c++ + c++ == 34) ){    printf("%d", c);}      This should print 19 every time. Basically what it does is this, from left to right:                  if (c == c){   c++; c++;   if (c == c)   {      c++; c++;      if (c == c)      {         ++c; ++c;         if (c == c)         {            ++c; ++c;            if (c == c)            {                c++; c++;               if (c == c)               {                  c++; c++;                  if (c == c)                  {                     ++c; ++c;                      if (c == c)                     {                        ++c; ++c;                        if (c == 17)                        {                           if (c + c == 34)                           {                              c++; c++;                              printf("%d", c );                           }                        }                     }                  }               }            }         }      }   }}    



I belive the only behavior with increment operators that is not defined is this:

c = c++;

Which will give you different results in the same compiler if you play with the optimization settings.



Edited by - krippy2k on June 29, 2001 11:07:25 AM

This topic is closed to new replies.

Advertisement