# TArray\<FColor\> backed by shared memory

**URL:** <https://forums.unrealengine.com/t/tarray-fcolor-backed-by-shared-memory/1764870>\
**Category:** C++\
**Tags:** tarray, memory-usage, memory-management, unreal-engine, question\
**Created:** [March 21, 2024, 10:56am UTC](https://forums.unrealengine.com/t/tarray-fcolor-backed-by-shared-memory/1764870 "2024-03-21T10:56:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![IAmNoBody2we](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@IAmNoBody2we](https://forums.unrealengine.com/u/IAmNoBody2we)\
**Post date:** [March 21, 2024, 10:56am UTC](https://forums.unrealengine.com/t/tarray-fcolor-backed-by-shared-memory/1764870/1 "2024-03-21T10:56:46Z")

</div>

Hello

I have the following code, it works well.

```C++
		TArray<FColor> PixelBuffer;
		RenderTargetResource->ReadPixels(PixelBuffer);

                // call a C API from .dll written in Rust.
		SendFrameFunc(PixelBuffer.GetData(), PixelBuffer.Num() * 4/*RGBA*/); 

```

And in my Rust code, the pixels are copied into a shared memory region and send to another process.

```auto
pub unsafe extern "C" fn send_frame(frame: *const ffi::c_char, size: u32) -> i32 {
    // ...
    let mut region = MemoryRegion::new(size);
    let dest = match region.map(..).unwrap();
    let dest_ptr = std::ptr::addr_of_mut!(*dest);
    std::ptr::copy_nonoverlapping(frame, dest_ptr as *mut i8, size);
    message.memory_regions.push(region);
    //...
}

```

The above code works fine except it is low effecient. Each frame size is 8M bytes. The frame is read into `PixelBuffer` then copied into the memory region.

I want the `ReadPixels` method directly load pixels into shared memory to avoid copying.  
I see `TArray` has a constructor `TArray ( const ElementType* Ptr, SizeType Count )` accept the memory address. I think I can use it to construct a` TArray` backed by my memory region.

1. I don’t need `TArray` free up my memory. Would it free up `ElementType* Ptr` on deconstruction? how can I avoid that? by implement my own Allocator?
2. I will initialize `TArray` with the exact memory size as the pixels needs. So I assume reallocation would never occur?
