16

Does a C++ shared library have its own memory space? Or does it share the caller process' one?

I have a shared library which contains some classes and wrapper functions. One of this wrapper function is kinda:

libXXX_construct() which initializes an object and returns the pointer to the said object.

Once I use libXXX_construct() in a caller program where is the object placed?Is it in the "caller" memory space or is it in the library's memory space?

7 Answers 7

11

A linked instance of the shared library shares the memory space of the instance of the executable that linked to it, directly or indirectly. This is true for both Windows and the UN*X-like operating systems. Note that this means that static variables in shared libraries are not a way of inter-process communication (something a lot of people thinks).

9
  • what if the executable that linked to the sharedlibrary is a shared library too? Is the object created in the inner .so in the same memory space of the main (which calls the latter .so)
    – nick2k3
    Jul 24, 2009 at 14:52
  • There is only ever one memory space.
    – anon
    Jul 24, 2009 at 14:53
  • True by default, but static variables can be used to share data on Windows. See my answer for details.
    – MSalters
    Jul 24, 2009 at 15:16
  • Well, they are certainly a BAD inter-process mechanism :-)
    – anon
    Jul 24, 2009 at 15:18
  • Why would anybody think that! Don't developers nowadays have a basic grasp of what the OS is doing for them! Jul 24, 2009 at 15:32
3

All the shared libraries share the virtual memory space of their process. (Including the main executable itself)

2

Unless specified otherwise, a shared library will share memory with the process hosting it. Each process instance will then have its own copy.

However, on Windows it is possible to create shared variables which allow inter-process communication. You do so by putting them in the right kind of segment. By default Windows uses two kinds of segments: data segments are read/write unshared, whereas code segments are read-only executable and shared. However, the read-write and shared attributes are orthogonal. A shared read-write segment in a library can be used to store shared variables, and it will survive until the last process exits.

Be careful with C++, as that will happily run constructors and destructors on process start & exit, even if you put variables in shared segments.

For details, see Peering Inside the PE: A Tour of the Win32 Portable Executable File Format part 2 by Matt Pietrek.

5
  • This is beyond the scope of C++ ( you are taking about fiddling at the assembly level). There are good reasons NOT to do this (specifically why they are not included as language constructs). Use the OS to provide inter-processes communication facilities. It is there to make that stuff safe. Jul 24, 2009 at 15:41
  • Shared libraries, which the question is all about, are beyond the scope of C++, as is IPC. C++ only has external or internal linkage, it has no notion of multiple programs running simultaneously, except arguably as relates to signals. Jul 24, 2009 at 15:58
  • @Martin York: Well, if you are trying to understand an existing program (and the question can be read to suggest that), you have to face the possibility that tghe previous author did indeed fiddle like this. You're mistaken as to the reason why they're not included in the language - that's because these tricks are inherently platform-specific and ISO C++ isn't.
    – MSalters
    Jul 27, 2009 at 8:09
  • 1
    Sorry I have a newbie question. How does shared library save overall memory usage, compared to static library, since "Each process instance will then have its own (shared library code) copy (in each process's virtual memory)." ?
    – Rick
    Mar 8, 2020 at 13:57
  • 1
    Doesn't this behaviour use same amount memory as a static linked library?
    – Rick
    Mar 8, 2020 at 14:09
1

The shared library has the same address space as its host process. It has to be that way, or else you wouldn't be able to pass pointers from one module to another since they wouldn't be able to dereference them.

But although they are in the same address space, that doesn't mean they all use the same memory manager. The consequence is that if you provide a function that allocates memory on behalf of the caller, then you should provide a corresponding function to free that memory, say, libXXX_destroy().

8
  • 1
    The memory manager is the thing that appears to dynamically allocate memory for you when you do things like malloc() or now. This is only tangetially related to the library address space issue, as you can use different managers within the same .c or .cpp source file, quite apart from in different libraries.
    – anon
    Jul 24, 2009 at 15:15
  • @Neil. I think the author is referring to the problem when each DLL was statically linked to the runtime library. This resulted in each DLL basically doing it own memory management (thus one DLL could not deallocate memory allocated by another DLL). This problem has been long solved though with the use of a shared DLL for the runtime libs. Jul 24, 2009 at 15:39
  • @martin I was just trying to defuse a possible red-herring. The basic question was about memory spaces, not memory allocation for specific languages.
    – anon
    Jul 24, 2009 at 15:42
  • Yes, Neil, the problem can occur without shared libraries, but there's more potential with them. Martin, the problem is solvable, but that's not the same as solved. They may link to different versions, or they may link to different vendors' runtimes. The library consumer might be in an entirely different language, unable to call delete. And finally, Neil, it may go beyond the original question, but I think it's still worth mentioning in this context since it's important to remember that having the same address space doesn't imply having the same memory pool. Jul 24, 2009 at 16:24
  • I seriously doubt that an implementation where you can end up with libraries linked against different and incompatible C++ libraries in the same memory space is a conformant implementation... Dec 21, 2010 at 4:38
0

Your object exists in the caller's memory space (in fact the one memory space shared between the library and the main executable)

0

The share address space so you can share pointers, however they don't share allocator (at least not on windows).

This means that if you call new to allocate an object inside a shared library you must call delete inside the same library or strange things may happen.

2
  • 1
    That problem has been solved. This was true for older development on windows, with modern development it is no longer valid problem. Just make sure you link all DLL to the shared version of the runtime. Jul 24, 2009 at 15:35
  • Thanks, I didn't know that have changed. The problems I had quite recently was most likely two instances of the static runtime library which is not shared between the executable and the dll.
    – Laserallan
    Jul 24, 2009 at 16:16
0

It's true that a library will use up memory in each process that loads it. However, at least under Windows, when multiple processes load the same DLL, unmodified pages (including all code pages) are quietly shared under the covers. Also, they take up no space in the swap file, since they're backed by the original file.

I believe this is more complicated for .NET, due to JIT compilation, but would still be true for NGENed assemblies.

edit

This is a detail of the VM. However, you can also flag a segment in a DLL to be shared across processes.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.