Singleton example by dave

const Root my_root = new(); // illegal
 Root my_root = Root::get();
 MyComponent me = new(“me”, my_root ); //race
 MyComponent me = new(“me”, Root::get() )

Is there static initialization order fiasco?
Dave said that it will appear.
But I don’t understand why line 3 has it.
In line 2 my_root should not be null because Root::get() must not return null.

If all these lines are static variable declarations, there is no defined order that these initializations take place as they are not executed as procedural code. That means it’s possible my_root has not executed its initialization before the initialization of me.

Why these lines will be static variable declaration when there is no static keyword before the variable?

They are static if declared at the top-level of module/interface/package. There is no other choice. If declared inside a procedural block, then there is a choice between static or automatic. If declared in a class, there is a choice between static or non-static (dynamically allocated when constructing the class).

So if I do this

 Root my_root = Root::get();
 MyComponent me;
me = new(“me”, my_root );

It can be guaranteed that the my_root in new() won’t be null?

You need to provide more context. If you mean

module top;
  Root my_root = Root::get();
  MyComponent me;
  initial me = new(“me”, my_root );

The static initialization call to Root::get() is guaranteed to happen before the initial process starts.

1 Like