News for hackers. Stuff that matters.
Re:Some obscure C features by userbinator on Thursday January 25, 2018 @08:21PM (#16236327)
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

Another interesting thing is that it relies on the linker ordering names in alphabetical order.

Re:Some obscure C features by kabdib on Thursday January 25, 2018 @08:34PM (#16236377)
Yeah, 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.
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @01:48AM (#16237457)
It 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.

Re:Some obscure C features by dbdr on Friday January 26, 2018 @03:41AM (#16237786)
I 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.

Re:Some obscure C features by aardvark179 on Friday January 26, 2018 @04:52AM (#16238017)
It'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.
Re:Some obscure C features by rwmj on Friday January 26, 2018 @03:27AM (#16237738)
No it's not. OCaml gets it right.
Re:Some obscure C features by pjmlp on Friday January 26, 2018 @03:34AM (#16237755)
By forcing developers to remember which order they should be linked?