The behavior of static variables in dynamically linked libraries (C / C ++)

As discussed here , a static variable is stored in the .BSS or .DATA segment.

Where is this memory stored if the static variable is inside a function that is in a dynamically linked library? Is storage for this variable stored in the .BSS or .DATA segment of the binding process during the connection?

+7
c ++ static memory shared-libraries
source share
2 answers

The static variable will be completed in the .BSS or .DATA section of the DLL file. An executable that references a DLL probably does not even know that it exists. When the EXE loads the DLL, the system sets up the DLL data sections for it, and then calls DllMain (). This is when static DLL statistics arise and is initialized.

+7
source share

Yes. Differences between different static variables:

  • region

  • initialization time for dynamically initialized.

The implementation (and note the BSS and DATA segments are implementation details) are usually the same. To ensure the correct initialization of dynamically initialized variables of a static function, one of the ways is to add an additional logical value indicating the need for dynamic initialization.

+1
source share

All Articles