Bug 41384 - initialization failure of global const variable of template class which has a constructor initializing its member which is a pointer to member data when compiled with -O2
Summary: initialization failure of global const variable of template class which has a...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.6
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-09-17 02:49 UTC by Gyusik Choe
Modified: 2009-09-17 09:47 UTC (History)
1 user (show)

See Also:
Host: x86_64-redhat-linux
Target: x86_64-redhat-linux
Build: x86_64-redhat-linux
Known to work: 4.3.2
Known to fail: 3.4.6
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gyusik Choe 2009-09-17 02:49:29 UTC
I wrote the following code.

------ begin file haha.cc ---------------
struct C {
        int i;
};

template <typename T>
struct A {
        A() : p(&C::i) {}
        int C::*p;
};

const A<int> r;

int main()
{
        return 0;
}
------------ end file haha.cc -----------------

And I compiled it with -O2 switch. The exact compilation command given was:
$ g++ -O2 -Wall haha.cc

The compiler didn't produce any errors nor warnings. But when I ran the generated binary, 'Segmentation fault' came up.

If you (1) make struct A a non-template class or (2) define r as non-const or (3) declare A::p as a normal pointer or (4) don't give -O2 switch, it compiles and runs well.

The following is the output from 'gcc -v' command:
$ gcc -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-10)
Comment 1 Richard Biener 2009-09-17 09:47:21 UTC
GCC 3.4.6 is no longer maintained.  Please report to your distributor.