3 ms·
That's also how C++ static constructor/destructor calls work --- a section contains entirely function pointers, which the init/uninit code in the RTL calls befo
by userbinator 9y ago
That's also how C++ static constructor/destructor calls work --- a section contains entirely function pointers, which the init/uninit code in the RTL calls before and after main().
https://msdn.microsoft.com/en-us/library/bb918180.aspx https://msdn.microsoft.com/en-us/library/bb918180.aspx
Another interesting thing is that it relies on the linker ordering names in alphabetical order.
- kabdib 9y agoYeah, the Macintosh Programmer's Workshop (MPW) did pretty much the same thing (back in 1987 or so). I forget the ordering guarantee, it might have been lexicographic by filename. Global initialization in C++ has been a horrible mess, portability-wise, for literally decades.
- pjmlp 9y agoIt is a mess in any language that allows it. The initialization order is implementation dependent in module/package initialization blocks. The only guarantee is that the modules being imported run first than the one being executed.
- rwmj 9y agoNo it's not. OCaml gets it right.
- pjmlp 9y agoBy forcing developers to remember which order they should be linked?
- dbdr 9y agoI believe the JVM specification describes precisely when class static initializers run, so that should not be implementation dependent. Of course it still won't be deterministic if you load classes in parallel in multiple threads.
- aardvark179 9y agoIt's true the JVM specifies this, but it doesn't mean it's always easy or nice to figure out what's going on. For example class A { static final int x = B.y; static final int y = 1; } class B { static final int x = A.y; static final int y = 2; } will not produce any static initializers and will do what you probably wanted because everything can be turned into a class constant and resolved at compile time. However this class A { static final Integer x = B.y; static final Integer y = 1; } class B { static final Integer x = A.y; static final Integer y = 2; } will not work the way you might want because there is no order in which the static initializers can be run that will do everything in the correct order.