Bug report

Aleksey Grinchenko grichenk@ncbi.nlm.nih.gov
Tue Mar 20 12:44:00 GMT 2001

Dear GNU.

I have found that the way GNU C++ destroys static variables
breaks the ISO/IEC standard rules. The standard says:

"Destructors for initialized objects of static storage duration
(declared at block scope or at namespace scope) ... are destroyed
in the reverse order of the completion of their constructor ..."

A simple program demonstrates, that GNU C++ compiler destroys
local static objects before destroying any global static object
instead of keeping the exact reverse order.

=== EXAMPLE ===
#include <iostream.h>

class CStatic
    CStatic(int id) : m_ID(id) { cout << m_ID; }
    ~CStatic(void) { cout << m_ID; }
    int m_ID;

int get2(void);
int get3(void);

static CStatic s1(1);
static int g2 = get2();
static int g3 = get3();
static CStatic s4(4);

int get2(void)
    static CStatic s2(2);
    return 2;

int get3(void)
    static CStatic s3(3);
    return 3;

int main()
    cout << "\n";
    return 0;

The correct output from the above program should be


while the gcc v. 2.95.2 19991024 (release) produces


(by the way, note, that s5 object is destroyed BEFORE executing
"cout << "\n" - that is not good also). I've got the same result
on different platforms, so I think it's a general gcc bug.

Thank you.
Aleksey Grichenko.


Those who don’t drink or smoke will die healthy.

More information about the Gcc-bugs mailing list