Returning vectors from a managed dll

H

HealsJnr

Hi all,

Just wondering if there is any way to return a vector from a mixed mode
dll? I understand that it is quite easy to pass by reference to achieve
the same end, but i'd like to know if it is possible to return a
vector.

Cheers,
 
G

Guest

you should not do that even if possible considering memory management
problem:
Memory allocated by a DLL shall also be freed by itself.
If you return a vector from that DLL, you violated above rules.
 
T

Tamas Demjen

www.fruitfruit.com said:
Memory allocated by a DLL shall also be freed by itself.
If you return a vector from that DLL, you violated above rules.

In fact, you can do that if you ensure that all of the following is true:

* The DLL and the app are compiled with the same compiler and linker
settings, with the exact same version of the compiler and linker, and
using the exact same STL version

AND

* All modules are linked against the dynamic version of the runtime library

In this case you can allocate memory in a DLL and delete it in the main
app, and vice versa, because the memory allocator itself is in the same
DLL (the Microsoft runtime library). You must ship the correct Microsoft
DLLs with your app as dependencies.

If you can't meet all of those conditions, you're not able to use
std::vector in the exported DLL function declarations (whether a return
value or an input argument, it doesn't matter).

Also note that returning a vector from a function returns it by value,
which means a copy of the vector is created. It's more efficient to pass
the vector by reference in one of the input arguments:

void f(vector<int>& output);

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top