[comp.lang.c++] ordering of static constructors...

brian@uw-june.UUCP (Brian Bershad) (08/11/87)

What's the best way (is there a way) to specify the order in which
one's static constructors are called?

I am trying to use ostream from within a constructor X, but the need
for an ostream constructor is  getting picked up by munch after the need
for my static constructor X is found.  The result:
	segmentation violation
in X.

This is version 1.2, and it passes the static test cases that
come with the distribution.


Thanks,

mikem@otc.oz (Mike Mowbray) (08/12/87)

In article <2973@uw-june.UUCP>, brian@uw-june.UUCP (Brian Bershad) says:
> 
> What's the best way (is there a way) to specify the order in which
> one's static constructors are called?

You can't do it manually.

> I am trying to use ostream from within a constructor X, but the need
> for an ostream constructor is  getting picked up by munch after the need
> for my static constructor X is found.  The result:
> 	segmentation violation
> in X.
> 
> This is version 1.2, and it passes the static test cases that
> come with the distribution.

We have implemented a simple change to munch.c which fixes up most of
these problems. The trouble is that munch normally takes ctors in the
right order, but arranges for them to be called in the reverse order.
(This is correct for dtors.) This isn't guaranteed to work everywhere
all the time, but if you lorder & tsort your library modules, etc, it
should be okay. Here are the diff's to lib/static/munch.c :

< 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;



			Mike Mowbray
		    Systems Development
			|||| OTC ||

		    ACSnet:  mikem@otc.oz
		      UUCP:  {uunet,mcvax}!otc.oz!mikem 
		     Phone:  (02) 287-4104
		     Snail:  GPO Box 7000, Sydney 2001, Australia