Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is allocated my memory in a dynamically loaded library?

I have been wondering for a while.. According to this table: http://ilay.org/yann/articles/mem/process_map.png (sorry for the french part of it) memory is allocated in different memory spaces depending on which part of the program is allocating it.

Thus, if I create an object in my program, it will be allocated in the program. If allocate memory dynamically, it will be in the heap , and the "links" for the dynamically loaded objects.

My question is: Where does the memory dynamically allocated from the dynamically loaded libaries belong?

if I have library that contains this:

extern "C" Object* create_Object()
{
   return new Object();
}

class Object
{
    int a;
    int* b;
    ...
}

Will my object be allocated in the heap or in the links?

My guess would be the heap, but I have a piece of code that makes me doubt it. I am developing a Cpp script system, where my scripts are compiled as shared libraries, loaded into my program, and reloaded everytime a script has to be recompiled. The idea is to save the state of my scripts before unloading them, and restore their content in the new instance of the dynamically loaded object. If the fields of my dynamically loaded object are mapped in the heap, save the pointer to the data is enough, but if the created class is allocated in the "links" segment of memory, then when I restore the content, my pointers will point on unallocated memory, which will make my program totally unstable, which is the case...

So I don't know. where in the memory is my instance of "Object"? where is "a", that is not dynamically allocated? and where would "b"'s content if I allocate it dynamically, in my example?

I would expect that new allocates in the heap no matter where the new is called, and that the variable a and b from Object would be allocated in the heap too, since it is instanciated from a dynamically allocated object, but it is only speculations and I would like to know it for sure...

I also wonder what happen to my Object instance once I dlclose my shared library. once again if it is in the heap, I should not have any problem to acces my object, but since I don't have the symbols, I guess the object would be allocated but quite unsuable since I would not be able to resolve its members.

I hope you can help me with that =)

like image 636
lagarkane Avatar asked Oct 24 '25 18:10

lagarkane


1 Answers

There is no such thing as memory dynamically allocated from the dynamically loaded libaries. Memory allocated dynamically means it is allocated with a call to malloc. There's only one malloc in your program, and it is of no importance whether the call to it comes from the part of the program code which was loaded with your original executable or with a shareable object file.

Note: from practical perspective, new is typically implemented via a call to malloc, therefore, the same consideration applies here.

Note, that on Win32 the situation may be quite different: a DLL may have its unique malloc (and, of course, free) operating on its own heap. Memory malloc'd in such a DLL must then be free'd in the same DLL.

like image 78
ach Avatar answered Oct 26 '25 07:10

ach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!