A memory block holds values I'd like to use as an std::vector
. I currently copy the values over using std::memcpy
.
#include <cstdlib>
#include <cstring>
#include <vector>
int main()
{
const std::size_t count = 2097152;
float* ptr = (float*)std::malloc(count * sizeof(float)); // raw ptr for simplicity
// ...
std::vector<float> vec(count, 0);
std::memcpy(vec.data(), ptr, count * sizeof(float));
//...
std::free(ptr);
}
However the copying is unnecessary, since ptr
is no longer needed at this point. How can I make std::vector
reuse the memory and take normal ownership of it?