C++ using std::vector across boundaries

1.2k views Asked by At

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 ?

2

There are 2 answers

3
Lightness Races in Orbit On

Neither.

It's allocated in the virtual memory space of the process, whose code is a combination of the .exe and the .dll.

2
lcs On

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.