How can I copy TArray<uint8>& Bufffer separately?

I have a combined TArray& buffer like below:

buf

I want to copy this buffer into 2 TArray buffer, but it doesn’t work.
the way I tried, buffer A can be copied, but buffer B couldn’t be extract.

const TArray<uint8>& Buffer;
TArray<uint8> Buffer_A;
TArray<uint8> Buffer_B;

I tried using the memmove function as a possible method, but it didn’t work.

FMemory::Memmove(Buffer_A, Buffer.GetData(), buffferA_size); 
FMemory::Memmove(Buffer_B, Buffer.GetData() + buffferA_size ,bufferB_size); 

Is there anyway to split this buffer into 2 buffer?
thanks in advance.

There are ways to split a combined TArray & buffer into two separate TArray buffers in Unreal Engine.

One way to do this is by using the TArray::SetNum() function to resize the two new TArrays to the desired size, and then use TArray::Memcpy() function to copy the data from the original buffer to the two new TArrays. Here’s an example of how you can do this:

Buffer_A.SetNum(bufferA_size);
Buffer_B.SetNum(bufferB_size);

Buffer_A.Memcpy(Buffer.GetData(), bufferA_size);
Buffer_B.Memcpy(Buffer.GetData() + bufferA_size, bufferB_size);

Alternatively, you can use the TArray::Slice() function to extract a portion of the original buffer and assign it to the new buffer, this way you don´t need to resize the new buffer:

Buffer_A = Buffer.Slice(0, bufferA_size);
Buffer_B = Buffer.Slice(bufferA_size, bufferB_size);

Please keep in mind that the first method will copy the data from the original buffer to the new one, so any modifications you make to the new buffers will not affect the original buffer, while the second method will make the new buffer a reference to a slice of the original buffer, so any modifications to the new buffers will affect the original buffer.

Please let me know if you have any other question.

Thanks, Elias.
You made me some hint!
Finally it works:

Buffer_A.SetNum(bufferA_size);
Buffer_B.SetNum(bufferB_size);

FMemory::Memmove(&Buffer_A, &Buffer, BufferA_size);
FMemory::Memmove(&Buffer_B, &Buffer + BufferA_size, BufferB_size);

1 Like