Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!apple!bionet!agate!ucbvax!decwrl!sun!pitstop!sundc!seismo!uunet!munnari!otc!mikem From: mikem@otc.oz (Mike Mowbray) Newsgroups: comp.lang.c++ Subject: Re: Global constructors and C++ I/O streams; problems and questions. Message-ID: <467@otc.oz> Date: 24 Oct 88 10:20:16 GMT References: <5955@columbia.edu> Organization: OTC Development Unit, Australia Lines: 58 In article <5955@columbia.edu>, beshers@select.columbia.edu (Clifford Beshers) says: > The problem lies in the constructor of the global instance [...] > the reference to the cout stream occurs before [its] initialization ... This is a problem that recurs now and then. I will re-post my original answer to the problem here anyway, since it is quite some time since this question was asked. > Because global instances are initialized in the order in which they are > declared, this is expected; Actually, this is not quite true - they are usually called in the order in which they are linked, which is the reverse of what you want. > So, the question, is there any way around this? To fix it, you must have the source of munch. (Patch users will need to do the fix by figuring what changes are needed in patch). Here are the diff's: $ diff lib/static/munch.c.orig lib/static/munch.c.new 36,37c36,38 < sbuf* dtor; // list of constructors < sbuf* ctor; // list of destructors --- > sbuf* dtor; // list of destructors > sbuf* ctor; // list of constructors > sbuf* tail; // tail of list of constructors 63c64,69 < ctor = new sbuf(ctor,st); --- > register sbuf *newctor = new sbuf(NULL, st); > if (tail != NULL) > tail->next = newctor; > else > ctor = newctor; > tail = newctor; $ I.e: make the list of ctors in the opposite order instead. Examine what is going on in munch and you will see what I mean about the order of calling static ctors. Note that static dtors are OK since you want them to be called in reverse order to static ctors. To understand all this you will also need to examine the otehr files in lib/static - cfront is arranging for an _main() function to be called at the start of main(), which calls a list of ctor functions which munch sets up. Mike Mowbray Network R&D |||| OTC || ACSnet: mikem@otc.oz TEL: (02) 287-4104 UUCP: {uunet,mcvax}!otc.oz!mikem SNAIL: GPO Box 7000, Sydney, Australia