This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
program crashes on startup when compiled with 'recent' gcc
- From: Zdeněk Sojka <zsojka at seznam dot cz>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 05 Oct 2009 23:29:00 +0200 (CEST)
- Subject: program crashes on startup when compiled with 'recent' gcc
The following code crashes during initialization of globals. Has to be compiled by "recent" gcc snapshot - this problem first appeared sometimes between 20090611 and 20090618.
---- header.h ----------
struct A {
static int &Init()
{
static int &i = *new int();
return i;
}
A() { A::Init(); }
};
---- main.cpp ----------
#include "header.h"
static A a;
int main(int argc, char *argv[])
{
return 0;
}
---- file.cpp ----------
#include "header.h"
------------------
$ g++ file.cpp main.cpp -o a.out && ./a.out
Segmentation Fault (SIGSEGV)
Attached: preprocessed files, "g++ file.ii main.ii -o a.out && ./a.out" to reproduce the problem.
The order of files passed as parameters is important.
It should be valid C++ with defined behaviour, but I am not sure about that, so I am asking here instead of opening bug report.
(note that simple "static int i; return i;" doesn't have defined behaviour for non-primitive types (afaik))
# 1 "file.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "file.cpp"
# 1 "header.h" 1
struct A {
static int &Init()
{
static int &i = *new int();
return i;
}
A() { A::Init(); }
};
# 1 "file.cpp" 2
# 1 "main.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.cpp"
# 1 "header.h" 1
struct A {
static int &Init()
{
static int &i = *new int();
return i;
}
A() { A::Init(); }
};
# 2 "main.cpp" 2
static A a;
int main(int argc, char *argv[])
{
return 0;
}