# Costless transfer of a std::vector\<T\>'s data to a TArray\<T\>.

**URL:** <https://forums.unrealengine.com/t/costless-transfer-of-a-std-vector-t-s-data-to-a-tarray-t/2124316>\
**Category:** C++\
**Tags:** tarray, unreal-engine, vector, memory, CPP\
**Created:** [November 15, 2024, 9:54am UTC](https://forums.unrealengine.com/t/costless-transfer-of-a-std-vector-t-s-data-to-a-tarray-t/2124316 "2024-11-15T09:54:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![OctoberIC](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@OctoberIC](https://forums.unrealengine.com/u/OctoberIC)\
**Post date:** [November 15, 2024, 9:54am UTC](https://forums.unrealengine.com/t/costless-transfer-of-a-std-vector-t-s-data-to-a-tarray-t/2124316/1 "2024-11-15T09:54:51Z")

</div>

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?
