Sign in to follow this  

DLL shared memory

This topic is 4661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to pass a pointers rather than a copy of data or C++ objects between DLL’s and the program that linked to them? I’m sure it is but I just wanted to know that before I started working with them, so I know what to expect.

Share this post


Link to post
Share on other sites
Yes. You only can't pass pointers if you're passing to another EXE, since it has a different address space. When a DLL is loaded, it shared the same address space as the EXE.

EDIT: Be careful about passing STL containers between a DLL and an EXE. You can't allocate memory in the EXE (with new, malloc, etc) and free it in the DLL (with delete, free, etc) or vice versa. If you must pass STL objects between the EXE and DLL, make sure you only pass by const reference, and never return by value.

Share this post


Link to post
Share on other sites
Quote:
Original post by MrRage
Ah ok, so the DLL can't directly manage memory on the EXE side of the application, or the EXE manage memmory for the DLL.
Exactly. You can still change memory in the EXE from the DLL (via a pointer), but you can't reallocate memory.
The reason for this is that under Windows, the DLL and the EXE have seperate heaps. If you really need to allocate memory in the DLL and free it in the EXE, you can link both the EXE and DLL to the DLL version of the CRT. Then new and delete are called from within the context of the CRT DLL, and everyone (well, Windows) is happy [smile]

Share this post


Link to post
Share on other sites

This topic is 4661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this