This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/20053] New: casting pointers to arrays in c++ produces wrong output when optimizing
- From: "linuxadmin at yandex dot ru" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 18 Feb 2005 11:52:45 -0000
- Subject: [Bug c++/20053] New: casting pointers to arrays in c++ produces wrong output when optimizing
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
To: gcc-gnats@gcc.gnu.org
Subject: casting pointer to array in c++ produces wrong output when optimizing
From: linuxadmin@yandex.ru (Ivan G. Shevchenko)
Reply-To:
Cc:
>Submitter-Id:
>Originator: Ivan G. Shevchenko
>Organization: -
>Confidential: no
>Synopsis: casting pointers to arrays in c++ produces wrong output when
optimizing
>Severity: critical
>Priority: medium
>Category: c++
>Class: wrong-code
>Release: 3.4.3 AND 4.0.0 20050213 (experimental)
>Environment:
System: Linux acid 2.6.10-my #1 Fri Feb 11 12:13:56 UTC 2005 i686 athlon i386
GNU/Linux
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-sources/configure --program-suffix=snapshot
--enable-libstdcxx-debug --enable-libstdcxx-debug-flags=-w -O0 -ggdb3
>Description:
accessing an uint32_t array (that is casted from an uint64_t pointer)
from an overloaded operator+ leads to wrong results when optimizing
with -O3, but produces correct results when optimizing with -O1
>How-To-Repeat:
#include<cstdlib>
#include<iostream>
using namespace std;
class TEST{
uint64_t data_;
public:
uint32_t& _0() {return ((uint32_t*)&data_)[0];}
const uint32_t& _0()const{return ((uint32_t*)&data_)[0];}
uint32_t& _1() {return ((uint32_t*)&data_)[1];}
const uint32_t& _1()const{return ((uint32_t*)&data_)[1];}
/*
// same effect
uint32_t& _0() {return *((uint32_t*)&data_+0);}
const uint32_t& _0()const{return *((uint32_t*)&data_+0);}
uint32_t& _1() {return *((uint32_t*)&data_+1);}
const uint32_t& _1()const{return *((uint32_t*)&data_+1);}
*/
};
TEST operator+(const TEST& a,const TEST& b){
TEST temp;
temp._0()=a._0()+b._0(); temp._1()=a._1()+b._1();
return temp;
}
int main(){
TEST a,b;
a._0()=a._1()=1;
b._0()=b._1()=2;
cout<<a._0()<<" "<<a._1()<<" "<<b._0()<<" "<<b._1()<<endl;
a=a+b;
cout<<a._0()<<" "<<a._1()<<" "<<b._0()<<" "<<b._1()<<endl;
b=b+a;
cout<<a._0()<<" "<<a._1()<<" "<<b._0()<<" "<<b._1()<<endl;
return EXIT_SUCCESS;
}
Following output is produced when compiling with gcc4.
Compiling with gcc3.4.2 from MinGW and gcc3.4.3 has similar effect.
I beleave that this is a bug, because I hadn't problems with the
free intel compiler for linux with any optimizations switches.
This program should produce the following output:
(it does so with CFLAGS='-march=athlon -O1')
1 1 2 2
3 3 2 2
3 3 5 5
But with CFLAGS='-march=athlon -O3' it produces:
1 1 2 2
1 1 2 2
3 0 2 2
And with CFLAGS='-march=i486 -O3' it produces:
1 1 2 2
1 1 2 2
3 3 2 2
>Fix: no idea
--
Summary: casting pointers to arrays in c++ produces wrong output
when optimizing
Product: gcc
Version: 4.0.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: linuxadmin at yandex dot ru
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20053