Tuesday, April 22, 2008

Static Variables in Shared Libraries

I haven't posted for a while, and this isn't really a deserving post, so I apologize for that.

This is however, something I can never seem to keep straight in my head, so I'm throwing it out here to keep track of it for myself and possibly others.

How Static Variables within Shared Libraries (C or C++) Work Under Linux
Static variables within shared libraries for the most part work exactly as they do directly in an executable, on a per process basis. There is no consistency of value between multiple processes using the same shared library.

Each process will have all static data within these shared libraries initialized to zero, and then their scope will be maintained throughout the lifetime of that process, independent of other processes.


I have tested this empirically and found it to be true, and in line with how I theorized that it worked. If you know otherwise and can explain it to me (and shatter my understanding of the world) - I would love to hear it.