DLL shared memory

Started by
3 comments, last by mishikel 19 years, 1 month ago
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.
Advertisement
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.
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.
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]
An EXE and a DLL have separate heaps only when not linked against a CRT DLL.

This topic is closed to new replies.

Advertisement