https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63364
Bug ID: 63364
Summary: GCC optimizer causing memory corruption
Product: gcc
Version: 4.8.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: contact at ncomputers dot org
http://ncomputers.org/content/code.php?src=findings/gcc%20optimizer%20memory%20corruption.cpp
We found that probably some optimization flags of the GCC compiler are causing
memory corruption after the reading of a variable's content through a pointer
to a class or a struct.
We've tested and confirmed this bug with these compiler's versions: 4.7.2 and
4.8.2
#include<iostream>
using namespace std;
struct A{
unsigned int const v;
A():v(10){}
void test_A();
};
struct B{
A const*const&a;
B(A const*const&aa):a(aa){}
void test_B(){
unsigned int it=0;
cout<<"Value of constant a->v: "<<a->v<<endl;
do{
cout<<it<<',';
}while(++it<a->v);
cout<<endl<<"Value of constant a->v: "<<a->v<<endl;
//cout<<endl<<"Value of constant a->v:"<<' '<<a->v<<endl;
/*
* GNU Compiler with -O2 or -O3 flag
* Value of constant a->v changes to zero
*
* If this result was shown:
*
* Value of constant a->v: 10
* 0,
* Value of constant a->v: 0
*
* Now comment the previous "cout" line and uncomment the next "cout"
line.
* Note that this is the only difference between both lines:
* <<' '
*
* The right result should be shown:
*
* Value of constant a->v: 10
* 0,1,2,3,4,5,6,7,8,9,
* Value of constant a->v: 10
*/
}
};
void A::test_A(){
B*b=new B(this);
b->test_B();
delete b;
}
int main(){
A*a=new A();
a->test_A();
delete a;
return 0;
};