Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ using std::vector across boundaries [duplicate]

Assuming that EXE and DLL use the same compiler and STL version. If I use a std::vector in my EXE and use reserve to reserve memory. Then I pass it as reference to a DLL.

I do a push_back in the DLL to add an element to my vector. If I do not exceed actual capacity, is the memory of the new element allocated in the DLL or in the EXE ?

like image 752
FSeywert Avatar asked Jun 08 '15 15:06

FSeywert


1 Answers

This is generally a bad idea.

When you call push_back, a copy can be made of whichever object you are adding to the vector. There is no guarantee that the size of that object (among other things) is the same as the size reserved in the .exe via std::vector::reserve. Both binaries may have been compiled with a different version of the STL.

like image 88
lcs Avatar answered Oct 10 '22 00:10

lcs