This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Constructors and destructors of static variables
- To: egcs at egcs dot cygnus dot com
- Subject: Constructors and destructors of static variables
- From: Adrian Trapletti <Adrian dot Trapletti at wu-wien dot ac dot at>
- Date: Wed, 17 Feb 1999 18:14:48 +0000
I have the following problem:
Suppose we have the three files:
main.cc:
-------------------------------
#include "X.hh"
int main ()
{
X x;
}
-------------------------------
X.hh
-------------------------------
class X
{
public:
X ();
~X ();
};
class Y
{
public:
Y ();
~Y ();
};
--------------------------------
X.cc:
-------------------------------
#include <iostream.h>
#include "X.hh"
static Y y;
X::X()
{
cout << "constructor X" << endl;
}
X::~X()
{
cout << "destructor X" << endl;
}
Y::Y()
{
cout << "constructor Y" << endl;
}
Y::~Y()
{
cout << "destructor Y" << endl;
}
--------------------------------
Now compilation, static linkage and execution yield:
33: g++ -c X.cc
34: g++ -o main main.cc X.o
35: main
constructor Y
constructor X
destructor X
destructor Y
While compilation, shared linkage and execution yield:
45: g++ -c X.cc
46: ld -shared -o X.so X.o
47: g++ -o main main.cc X.so
48: main
constructor X
destructor X
Is that what we want, or is this a bug? What about y,
is there some memory reserved for y? What are the consequences?
Environment:
Redhat 5.2
49: g++ -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.90.29/specs
gcc version egcs-2.90.29 980515 (egcs-1.0.3 release)
50: ld -v
GNU ld version 2.9.1 (with BFD 2.9.1.0.15)
Thank you for comments and suggestions
Adrian
--
Adrian Trapletti, Vienna University of Economics and Business
Administration, Augasse 2-6, A-1090 Vienna, Austria
Phone: ++43 1 31336 4561, Fax: ++43 1 31336 708,
Email: adrian.trapletti@wu-wien.ac.at