Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2005

Tip: Looking for answers? Try searching our database.

Returning vectors from a managed dll

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
HealsJnr - 23 Sep 2005 07:29 GMT
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,
www.fruitfruit.com - 27 Sep 2005 13:58 GMT
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.

> Hi all,
>
[quoted text clipped - 4 lines]
>
> Cheers,
Tamas Demjen - 27 Sep 2005 19:30 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.