Thread: Global variable in shared library

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    11

    Global variable in shared library

    If a shared library uses a global variable that is not exported, does each process that uses that library get a copy of that variable?

    For example, in the library in the source for one of the object files:

    Code:
    static struct foo * ptr;
    ptr is only used within that file, which is obvious because it is static. So, what I don't understand is if there is only one copy of ptr or if there is one for each process that is using the shared library?

    My initial thought would be that there are indeed multiple copies but I can't seem to get a definitive answer.

    Either way, there's only one copy for all threads that a single process creates, correct? So, I do need to make sure everything is thread safe, but that's another topic.

    Thanks for any help.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by krock923 View Post
    If a shared library uses a global variable that is not exported, does each process that uses that library get a copy of that variable?
    Of course. Data is specific to processes, not libraries.

    My initial thought would be that there are indeed multiple copies but I can't seem to get a definitive answer.
    Correct.

    Either way, there's only one copy for all threads that a single process creates, correct?
    Correct.

    Only CODE is shared, not data (well, constant data might be shared)

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    Thanks

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In practice, what brewbuck says is usually true. However, technically, it depends on how the library is built (linker settings, what segment particular types of data are put into, etc) and on the operating system. With some older systems, which have less emphasis on security than modern systems, a static in a shared library was one way of sharing data between processes.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    Sigh, what a pain. I'm probably better off just rewriting the thing to get rid of the global, even if it makes the interface less convenient.

    I might be actually better off using C++

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by krock923 View Post
    Sigh, what a pain. I'm probably better off just rewriting the thing to get rid of the global, even if it makes the interface less convenient.
    I'd agree with that, regardless of whether you're building a shared library or not. Use of globals imply various trade-offs in terms of code reuse (and that directly affects usability of a shared library), ability to use in a multi-threaded environment, ability to call functions recursively, ability to use the same code multiple times within the one program, etc etc. There are certainly some circumstances where a global is necessary but, if possible, it is better to remove the need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. Shared library design problem
    By p1r0 in forum C++ Programming
    Replies: 9
    Last Post: 03-23-2009, 12:36 PM
  3. error on using shared library
    By -EquinoX- in forum Tech Board
    Replies: 2
    Last Post: 05-06-2008, 04:42 PM
  4. Global Variable Usage -- Failure in assignment
    By jake123 in forum C Programming
    Replies: 7
    Last Post: 02-15-2008, 02:30 PM
  5. Class member using a global variable, causing problems.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 11:27 PM