When working with external libraries, one often has to cast from C++ types to Unreal Engine types with the exact same binary representation. Here for instance, boost::geometry’s point_xy and FVector2D. I don’t see why there really should be anything copied other than the respective C++ and UE containers’ headers. So in the following I manually cast a std::vector<point_xy> into a TArray without copying anything more than the std::vector’s twenty-four bytes header into the TArray’s header’s sxiteen bytes.
// std::vector 8 (ptr) 8 (size) 8 (capacity) => TArray 8 (ptr) 4 (size) 4 (capacity).
typedef boost::geometry::model::d2::point_xy Point2D;
std::vector source;
// Do stuff …
TArray target;
uint64* source_ptr = (uint64*)&source;
uint64* target_ptr = (uint64*)& target;// Gives the pointer.
target_ptr[0] = source_ptr[0];// Gives the new size and capacity formatted from two uint64 to two int32.
target_ptr[1] = ((uint64)(int32)source.capacity() << 32) | (int32)source.size();// Nullify the vector’s header.
source_ptr[0] = source_ptr[1] = source_ptr[2] = 0;
Which doesn’t cause a crash by itself. The crash happens a few ticks later when Unreal Engine’s GC realizes that someone tried to pull a fast one on him. How to prevent it?