Thread: Compiler reorder the statement to excute

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    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 07:51 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    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
    344
    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 09: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, 04:09 PM
  2. Does'nt excute program always crashes
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 10-30-2005, 06: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