4

My question is about static variable (static void*) created inside shared library (let's call this library 'S'), but it's an internal variable not shown outside, but every call of API depends on it. Now let's think about a case, when a program (let's call it main program) links to another two shared libraries and every one of them is linked with library S. Now what happens to this static variable for our main program? Does it have one instance? Two?

5
  • there 'll be one instance since you declared it as a static one Commented Feb 24, 2014 at 9:39
  • Related: stackoverflow.com/questions/3186926/… Commented Feb 24, 2014 at 9:58
  • @Suma How does answer to question you mentioned help me? Commented Feb 24, 2014 at 12:36
  • It is related in that it shows one scenarios, where linking to two shared libraries B and C both linking statically to library A can cause a variable in A to exist twice. Commented Feb 24, 2014 at 13:51
  • In this case there is no static linking. Commented Feb 24, 2014 at 16:10

3 Answers 3

7

Suma's answer is correct. There will only be a single instance of the static variable. This is also why having static globals in shared libraries can be a huge problem. One real-world example where this can happen:

  • Apache webserver which loads the following modules:
    • mod_php which is linked against
      • libxml2
    • mod_perl which loads
      • libxml2

Now if some PHP code modifies a global setting like a parser option in libxml2, the Perl code will also see these changes. This can lead to bugs that are extremely hard to diagnose. So you should avoid global state in your shared libraries at all cost.

(With libxml2 you can make most settings locally these days.)

Sign up to request clarification or add additional context in comments.

Comments

3

Assuming your static variable is only defined in one translation unit, it will exist only once, as the shared library is loaded only once into the process.

This would get more difficult if a mixture of shared and static linking was used.

Comments

0

The compiler creates a different instance for every global static variable, even when you have several such variables with identical names.

In fact, the compiler (or possibly, the preprocessor) implicitly changes the name of every such variable, according to the name of the source file which declares it.

You can prove this to yourself by declaring a global static variable in a header file, and then include this header file in several different source files. Try to set it to a different value in each source file, and you'll see that this variable retains its different value in each source file.

5 Comments

I do not think this is what this question is about. There is no reason to assume the static variable has multiple definitions in the code.
@Suma: Thank you for the down-vote. Please try the suggestion at the bottom of the answer, and I promise to remove this answer if you prove me wrong!!!
I understand what you write, and you are correct in that, but there is no reason to assume OP has defined the variable in a header, or in multiple translation units.
... and if the variable is defined only once, and if the library is linked only once (which it mostly will, unless you are mixing static / shared linking), then there will be only one instance of the variable.
@Suma: My answer explains the concept of a global static variable in general. The point of declaring it in a header file is merely in order to support the description I provide, and to suggest a means for testing it!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.