#726 ANOMALY IN COMPILERS

open
nobody
None
5
2012-09-27
2012-09-25
No

I think I have encountered an anomaly in all the compilers, certain faulty behavior in an area, which is there in the Dev-C++ too. In the following code:

int a, b=55, c=55, d, e=55, f=55, g;
a= (b>c);
d= (e>f);
g= (a++&&d++)+ 100;
cout <<"a: " <<a <<"\nd: " <<d <<"\ng: " <<g;

the results can be altered just by placing the operator "++" left and right, in the following ways (placements and their effects are in square brackets):

g= (a++&&d++)+ 100;
Result: a: 1, d: 0, g: 100.

g= ([++a]&&d++)+ 100;
Result: a: 1, [d: 1], g: 100.

g= (++a&&[++d])+ 100;
Result: a: 1, [d: 1], [g: 101].

g= (a++&&++d)+ 100;
Result: a: 1, d: 0, g: 100.

Only the first and the last lines produce accurate results. The middle two alter the results producing faulty results.

This was there in Borland 5.5, and in Visual C++ 2010 Express too.

I don't know about others, but all I am saying is that we can correct this behavior in our compiler, the Dev-C++.

Thanks

4.9.9.2

Discussion

  • HolyDoom Witch

    HolyDoom Witch - 2012-09-25

    Did I mention that I am always on 'auto-knack' and 'auto-penchant' modes of crashing into the flawed areas of ALL the things possible in this world?! ;)

     
  • HolyDoom Witch

    HolyDoom Witch - 2012-09-27

    Now I understand that this comes under something known as "Undefined Behavior", which again comes under something called "Sequence Points". But still I am not deleting the question, because may be it can still be rectified in the Dev-C++.


     
    Last edit: HolyDoom Witch 2012-09-27
  • HolyDoom Witch

    HolyDoom Witch - 2012-09-27

    I don't know why the square brackets get removed in the new site. They show up in editing but not after posting.

    It were the middle two lines:

    g= (++a&&d++)+100;
    Result: a: 1, d: 1, g: 100.

    g= (++a&&++d)+100;
    Result: a: 1, d: 1, g: 101.


     
    Last edit: HolyDoom Witch 2012-09-27
  • HolyDoom Witch

    HolyDoom Witch - 2012-09-27

    May be we could ban such expressions through the compiler, since what is intended through these expressions can be accomplished through many other codes.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks