Bug 31398 - When O2 optimization option is set, the execution flow is changed.
Summary: When O2 optimization option is set, the execution flow is changed.
Status: RESOLVED DUPLICATE of bug 11751
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.3
: P3 blocker
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-03-30 11:43 UTC by Bill.Fun
Modified: 2007-03-30 13:03 UTC (History)
52 users (show)

See Also:
Host: Intel platform
Target: Red hat linux, 2.6.9-5.EL
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
The sample code show the bug. (251 bytes, text/plain)
2007-03-30 11:47 UTC, Bill.Fun
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Bill.Fun 2007-03-30 11:43:02 UTC
Look out the output:
1. When no O2 option set, the output is:
   send out: 500000 Time: 1165250900
   192.168.1.1     100000

2. When -O2 option is set, the output is:
   192.168.1.1     send out: 500000 Time: 1165250934
   100000

It proves that the execution sequence has been changed. I think it's a block bug, due to the reason that I can not control the execution flow, I had disabled -O2 optimization, but that's a big performance discount to my program.


Source Code: 

#include <iostream>
#include <ctime>

float
CalcDownSpeed()
{
        // fake define
        int nStartTime = 100;
        int nTotalSendOut = 500000;
        int nLastCalc = 10000000;

        int now = time(NULL);

        std::cout << "send out: " << nTotalSendOut << " Time: " << now - nLastCalc << std::endl;

        float downspeed = 100000;

        return downspeed;
}

int main()
{
        std::cout << "192.168.1.1" << "\t" << CalcDownSpeed() << std::endl;
        return 0;
}
Comment 1 Bill.Fun 2007-03-30 11:47:41 UTC
Created attachment 13301 [details]
The sample code show the bug.
Comment 2 Richard Biener 2007-03-30 13:03:33 UTC
You are relying on undefined behavior.

*** This bug has been marked as a duplicate of 11751 ***