Bug 31398

Summary: When O2 optimization option is set, the execution flow is changed.
Product: gcc Reporter: Bill.Fun <windows2000d>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: blocker CC: alexey, algorithmus, asokumar, av1474, bala, barnarr, behloul.younes, bmead15, buergel, carpman, chuchunxin, devnull, d_picco, eric.mcvicker, fuchsia.groan, gaurav_har, gcc-bugs, gcc, ggs, jandres, janis, jesser, jompo, jroemmler, krs, lid, lindahlb, lxg8906, mayer, mikaldaz, mike.clarkson, nakkore, nobs, pierre.van.de.laar_at_philips.com, qyang, ramiller, raoulgough, raphael.ribas, rglan, rjvbertin, robc, s9322036, SimonX200, smartmouse714, suan, super.aorta, svetozarmarkov, tczarnecki, vanveghel, vitaly, windows2000d, zshao
Priority: P3    
Version: 3.4.3   
Target Milestone: ---   
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.

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 ***