Bug 25355 - reinterpret_cast<> yields different (and incorrect) results when using -O2
Summary: reinterpret_cast<> yields different (and incorrect) results when using -O2
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-12-11 17:54 UTC by Jason Elbaum
Modified: 2005-12-11 17:58 UTC (History)
54 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jason Elbaum 2005-12-11 17:54:24 UTC
The test case:

#include <iostream>

using namespace std;

int main()
{
    unsigned int test = 1067320345;

    cout << reinterpret_cast<float &>(test) << endl;

    cout << "reinterpret_cast<float &>(test): " <<
      reinterpret_cast<float &>(test) << endl;
}


When compiled using -g or -O1, the correct output is produced:

1.2345
reinterpret_cast<float &>(test): 1.2345


When compiled using -O2, the first reinterpret_cast<> produces incorrect results. The actual value produced depends on the OS:

-NaN
reinterpret_cast<float &>(test): 1.2345


I tested this on Solaris 8 and on Linux Redhat WS3, with identical results. My conclusion is that we're looking at an optimizer bug.
Comment 1 Andrew Pinski 2005-12-11 17:58:25 UTC
You are violating C/C++ aliasing rules.


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