Is dynamic loading strictly compliant with the C ++ standard?

Does using dynamic loading require any special precautions for strict C ++ code?

The C ++ 11 standard refers to the ordering of certain events, such as what happens before the first call to main (). Dynamic loading, however, seems to pull the carpet out of typical assumptions about the ordering of events in a program.

As an example, here is a quote from § 3.3.2.

Static initialization must be performed prior to any dynamic initialization.

In the case of dynamic loading, this seems like an almost impossible commitment if taken literally. The program can do dynamic initialization and then load the code dynamically. If this code contains variables that were usually statically initialized, the C ++ standard was violated. It seems possible that the order of events in the Standard may still be satisfied and legal as is, but elsewhere that others have warned about interpreting this rule too broadly.

+3


source to share


1 answer


There is no provision for dynamic modules in the C ++ standard, so some interpretation is required.

Yes, statically initialized variables in dynamically loadable modules will be initialized after dynamically initialized variables in the main module. You can observe this and create programs where it affects the behavior of the program. But if you think of a DLL as a separate program that shares the main program memory space but has its own timeline, you can pretty much apply the same rules at the module level and use them to predict application-level behavior. The compiler doesn't want to surprise you ... it's just sometimes.



By the way, initialization order is the least of your problems when it comes to collisions between C ++ and DLLs. Dynamic modules break a lot more rules than that, especially when it comes to RTTI.

+3


source







All Articles