I am trying to implement the directions in these two SO posts regarding assigning containers to specific memory locations on the heap:
- Moving C++ objects, especially stl containers, to a specific memory location
- Create objects in pre-allocated memory
However, I must be doing something stupid (I'm new to C++) because I can't even get my code to compile (using C++ 11 as you can see below):
std::vector<int> ints1 = {10, 20, 30};
ints1.reserve(20);
auto adr = &(*ints1.end());
auto *ints2 = new(adr)(std::vector<int>(20);
I am getting an error of a expected , or ; before )
for the last line, which I take to mean that my syntax is just wrong, but how?
Also, once I fix this syntax, what's the best way to ensure these objects really are contiguous? I thought I would ensure that
&(*ints1.begin()) - &(*ints2.begin) is roughly on the order of sizeof(std::vector<int> v(20));
Is there a better way?
(p.s. I do realize I should allocate the memory I am going to use to do this to avoid undefined behavior)
Aucun commentaire:
Enregistrer un commentaire