Like Tree1Likes
  • 1 Post By iMalc

Compiler reorder the statement to excute

This is a discussion on Compiler reorder the statement to excute within the C++ Programming forums, part of the General Programming Boards category; I'm reading an ebook. they're using volatile keywords for a variable. This is my example code: Code: volatile boolean check; ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Compiler reorder the statement to excute

    I'm reading an ebook. they're using volatile keywords for a variable.
    This is my example code:
    Code:
    volatile boolean check;
    int a ;
    //other function
    public void functionA(){
         check = true;
         while(a>0){ a--;}
    }
    They explain check variable is volatile because: the compiler might decide to reorder the statements in function A if it recognizes that there are no dependencies. It is allowed to do this if it thinks it will make the code execute faster
    It means in real, this code might run like:
    Code:
    while(a>0){ a--;}
    check = true;
    Does this statement true ? I feel very strange of this because I have never heard this before.

    thanks
    Last edited by hqt; 02-04-2012 at 06:51 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Yes, optimisation is allowed as long as the compiler can determine that the result will produce observable behaviour that is the same as the observable behaviour without the optimisation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    However, the ebook is misinforming you. The volatile keyword is insufficient to prevent the compiler performing those optimisations. There is no guarantee that check must be set true before a is changed.

    It could also change the code to
    Code:
        check = true;
        a = 0;
    or to
    Code:
        a = 0;
        check = true;
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    It can't quite optimise that loop to a=0; because a might start out negative. However something simewhat similar that accounts for that could be used, assuming it hasn't proved that a is never negative.

    But yeah there be dragons ahead. I'm not sure you want to get mixed up in this stuff. Take the blue pill.
    AndrewHunter likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    Edit - duh, never mind. A decent compiler will conditionally set a to 0 if it's greater than 0 and leave it alone for negative values. No need to run the loop, though.
    Last edited by KCfromNC; 02-06-2012 at 08:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does the code excute, but I get no print message?
    By cdalten in forum C Programming
    Replies: 3
    Last Post: 02-11-2006, 03:09 PM
  2. Does'nt excute program always crashes
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 10-30-2005, 05:45 PM
  3. Compile ok,Excute error,why???
    By Lorin_sz in forum C++ Programming
    Replies: 4
    Last Post: 05-29-2005, 10:22 AM
  4. compiler skips one statement
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 08:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21