views:

763

answers:

7

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?

+2  A: 

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

EFraim
A: 

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

Draemon
A: 

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.

Laserallan
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.
Martin York
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
+3  A: 

A linked intance 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 means of inter-process communication - something a lot of people think is the case.

anon
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
There is only ever one memory space.
anon
Thank you very much.
nick2k3
True by default, but static variables *can* be used to share data on Windows. See my answer for details.
MSalters
Well, they are certainly a BAD inter-process mechanism :-)
anon
Why would anybody think that! Don't developers nowadays have a basic grasp of what the OS is doing for them!
Martin York
@Martin That was arhetorical question I take it? My experience is - no they don't, ant more than they did in the past.
anon
I believe there's a qualification to this that we need to make. Check out my answer for details.
Steven Sudit
@Steven This is more a feature of virtual memory than anything else. It applies equally to single executables that don't link to shared libraries.
anon
@Neil, I think for your purposes, you only care if changes to the state of a library in one process affect the library in other processes, so the fact that they happen to share memory at the VM level is irrelevant. However, this underlying fact is what allows segments of memory to be shared. Check out http://msdn.microsoft.com/en-us/library/h90dkhs0(VS.80).aspx
Steven Sudit
A: 

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().

Rob Kennedy
could you explain further this memory manager stuff?
nick2k3
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
@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.
Martin York
@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
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.
Rob Kennedy
+2  A: 

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.

MSalters
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.
Martin York
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.
Steve Jessop
@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
A: 

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.

Steven Sudit