rfg@ics.uci.edu (Ron Guilmette) (11/22/89)
In article <8911172207.AA07691@toadwar.UCAR.EDU> gerry@TOADWAR.UCAR.EDU (gerry wiener) writes: >When the following code is compiled and executed, a segmentation >violation occurs. >------------------------------------------------------------------------ >#include <stream.h> > >class x >{ >public: > x() {cout << "initialize\n";} > ~x() {cout << "clean up\n";} >}; > > >x y; > >main() >{ > cout << "hello world\n"; >} >------------------------------------------------------------------------ > >If the line > >x y; > >is moved inside main() as in > >main() >{ > x y; > cout << "hello world\n"; >} Yes. the problem is that during the construction of *your* global static objects, the *library* global static objects called cin, cout, and cerr have not yet been constructed. This is because of the fact that the language does not define an order for the construction of global static objects. I have suggested that G++ be fixed (and it is a trivial fix) so that library global static objects are constructed *before* any of the "user's" objects, but I guess that hasn't happened yet. If you want to fix this yourself, just modify the two loops within the routines do_global_init and within do_global_cleanup (inside gnulib3.c) so that they each run in an order which is REVERSED to the order they currently run in (but with the same limits). This will allow you to (a) use standard streams in your constructors for global objects, and (b) use standard streams in your destructors for global objects. You'll like that! // rfg > >the executable correctly produces > >initialize >hello world >clean up