# Nested TArrays in structs and memory

**URL:** <https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416>\
**Category:** C++\
**Tags:** unreal-engine, CPP, question\
**Created:** [February 25, 2025, 2:13pm UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416 "2025-02-25T14:13:42Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![rokenrock](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rokenrock/32/1040844_2.png) [@rokenrock](https://forums.unrealengine.com/u/rokenrock)\
**Post date:** [February 26, 2025, 8:32am UTC](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416/3 "2025-02-26T08:32:22Z")

</div>

As Emaer said, memory allocation is relatively slow on the heap, so we reserve multiple elements at a time. This is pretty standard with dynamically resizing arrays- ie in the standard library, gcc implements vectors with a 1.5x allocation (so 3-\>4, 6-\>9, 10-\>15, 1000-\>1500) iirc.  
Bringing us back to your third array, the reason it’s so much bigger is because it allocates for 4 elements.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/1/0/81082beb5112ba2e672e2745dfc6efa15a05ef83.png)

By default (ignoring some optimizations), whenever the default sized allocator runs out of space, it will allocate according to this calculation:

```auto
const SIZE_T FirstGrow = 4;
const SIZE_T ConstantGrow = 16;
	
SIZE_T Grow = FirstGrow; // this is the amount for the first alloc

if (!bArrayIsEmpty)
{
	Grow = SIZE_T(NumElements) + 3 * SIZE_T(NumElements) / 8 + ConstantGrow;
}

```

Since this only happens on a resize, you can avoid this by explicitly reserving exactly how much you need, which is a good practice I see you already know of given the Reserve.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/5/c/55ccc461740b716c3b9c35b303f6e495dcb259e5.png)

 ![The array now only holds exactly that one value](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/e/7/ce7a7522f52b319f4211a5e91620bcba07d59cbd.png)

  

> [@Emaer](#):
>
> then remove 25, the array will still take up memory for 100 elements.

I do want to point out that this is actually up to the individual developer in most cases, and the default behavior for most of the remove functions is to shrink the array so it doesn’t hold onto that memory. Sometimes this is a bool, but most of the time it’s an EAllowShrinking.

---

_[View the full topic](https://forums.unrealengine.com/t/nested-tarrays-in-structs-and-memory/2357416)._
