简体   繁体   中英

Behaviour 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's in a dynamically linked library ? Does storage for this variable get allocated in the .BSS or .DATA segment of the linking process at the time of linkage ?

The static variable is going to end up in the .BSS or .DATA section of the DLL file. The executable that links to the DLL probably won't even know it exists. When the EXE loads the DLL, the system sets up the DLL's data sections for it, and then calls the DllMain(). That's when the DLLs statics come into existence and get initialized.

Yes. The differences between the different sort of static variables are:

  • the scope

  • the initialization time for those which are dynamically initialized.

The implementation (and note BSS and DATA segments are implementation details) is usually the same. To ensure the correct initialization of dynamically initilized static function variables, one way is to add an additional boolean one indicating that dynamic initialization is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM