Topic: C++ 3.0 static initializers invoke order on SunOS 4.x v. 5.3


Author: "David N. Kamber" <David.Kamber@ska.com>
Date: 1995/05/03
Raw View
exubern@exu.ericsson.se (Bernie Meier) wrote:
>Does anyone know if the SunOS 4.x and Solaris 2.3 (SunOS 5.3) C++ 3.x compilers
>do anything different to static initializers?

>
>What gives? Anyone have any idea?

There is no guarantee nor any standard for order of initialisation of statics.
That's the standard: you cannot assume anything.

There's a work around using initialiser classes.

See  Scott Meyers "Effectice C++" Item 47 p.178 or <iostream.h> on this.


>
>---
>Bernie Meier
>
>

--
Regards,

David

+----------------------------------------------------+
|                                                    |
|  David N. Kamber  |  Phone: +41 (0)1 333 74 75     |
|                   |  Fax:   +41 (0)1 332 39 75     |
|  CREDIT SUISSE    |  E-mail: David.Kamber@ska.com  |
|  Dept. Ohm3       |                                |
|  CH-8070 Zurich   |  CompuServe: 100604,3105       |
|                                                    |
+----------------------------------------------------+
|    SCHWEIZERISCHE KREDITANSTALT / CREDIT SUISSE    |
+----------------------------------------------------+






Author: exubern@exu.ericsson.se (Bernie Meier)
Date: 1995/05/01
Raw View
Does anyone know if the SunOS 4.x and Solaris 2.3 (SunOS 5.3) C++ 3.x compilers
do anything different to static initializers?

Specifically, I have some code that has several static objects that are created
before main() (similar to the cout and cin stream objects), which have a specific
interdependency.

As far as I understand this mechanism, the C++ 3.x compiler generates a static
linked list which is patched at link-time, which contains pointers to the static
object initializers (contstructors). At run time, this list is traversed as
the first thing in main() (before any other user code), each contstructor called
in the order found in the list.

On SunOS 4.1.3, the SC2.0.1 (C++ 3.x) compiler generates a certain (correct) list
order, which, incidentally, seems to be controlled by a combination of the ordering
of object initializer declarations, and the ordering of object file (.o) inclusion in
the final executable ouput.

However, on Solaris 2.3/SunOS 5.3, the SC2.0.1 (C++ 3.x) compiler generates a
seemingly different order, because when executed on a Solaris machine, the identical
code crashes - precisely because an initializer was called when the object it
depended on was not yet instantiated!

The two executables are generated in the exact same manner, using the exact same
make file, with the exact same paths (except for the C++ headers/libs), just
on different machines running the two OSs.

What gives? Anyone have any idea?

---
Bernie Meier