# Why is my multidimensional array implementation crashing on Init?

**URL:** <https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656>\
**Category:** C++\
**Tags:** tarray, unreal-engine, question, CPP\
**Created:** [May 18, 2014, 2:39pm UTC](https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656 "2014-05-18T14:39:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![xlar8or](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xlar8or/32/710746_2.png) [@xlar8or](https://forums.unrealengine.com/u/xlar8or)\
**Post date:** [May 18, 2014, 2:39pm UTC](https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656/1 "2014-05-18T14:39:41Z")

</div>

Hello, i’m making my custom 2D Array implementation and i’m using the following code:

```
template <typename T>
class TArray2D
{
	TArray<TArray<T>> Array;

	uint32 Rows;
	uint32 Columns;

public:

	void Init(uint32 Rows, uint32 Columns, T Value);

	uint32 GetRows() const;
	uint32 GetColumns() const;

	TArray<T>& operator[](uint32 Row);
};

template <typename T>
void TArray2D<T>::Init(uint32 Rows, uint32 Columns, T Value)
{
	Array.Init(Rows);

	for (uint32 Row = 0; Row < Rows; Row++)
		Array[Row].Init(Value, Columns);

	this->Rows = Rows;
	this->Columns = Columns;
}

```

When i initialize my TArray2D using the Init method the game crashes on the for loop, more specifically on the following line from TArray::Empty when it’s doing the init of the sub array:

```
AllocatorInstance.ResizeAllocation(0,ArrayMax,sizeof(ElementType));

```

Can you tell me what am i doing wrong? Thank you

---

<div class="post-metadata">

**Author:** ![xlar8or](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xlar8or/32/710746_2.png) [@xlar8or](https://forums.unrealengine.com/u/xlar8or)\
**Post date:** [May 18, 2014, 8:28pm UTC](https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656/2 "2014-05-18T20:28:00Z")

</div>

If i use this:

```
for (uint32 Row = 0; Row < Rows; Row++)
{
	TArray<T> Temp;

	Temp.Init(NULL, Columns);

	Array.Add(Temp);
}

```

instead of:

```
Array.Init(Rows);
 
for (uint32 Row = 0; Row < Rows; Row++)
    Array[Row].Init(Value, Columns);

```

It appears to work (although with Add it creates more positions than i need), but i would like to know why the last method doesn’t work.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [May 18, 2014, 8:31pm UTC](https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656/3 "2014-05-18T20:31:05Z")

</div>

#2D TArrays: Complete Code Sample

I posted a full code sample on 2D TArray’s here

> **[SaxonRahs Tutorial Thread - Random Maze Generation & Solving](https://forums.unrealengine.com/showthread.php?47-SaxonRahs-Tutorial-Thread-Random-Maze-Generation-amp-Solving&p=27139&viewfull=1#post27139)**
>
> Welcome to my tutorial thread. This maze generator is dedicated to Rama; as in I’m providing it for free. Since he’s helped me so much. \<3 Thanks mate. Here’s some videos of what were going to do. Random Maze...

#Add Uninitialized

Take careful note of the use of AddUninitialized at the end of my example above.

Rama

---

<div class="post-metadata">

**Author:** ![xlar8or](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xlar8or/32/710746_2.png) [@xlar8or](https://forums.unrealengine.com/u/xlar8or)\
**Post date:** [May 18, 2014, 8:44pm UTC](https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656/4 "2014-05-18T20:44:55Z")

</div>

Based on [this link](https://docs.unrealengine.com/latest/JPN/API/RuntimeModules/Core/Internationalization/FTextLocalizationManager/RegenerateResour-/index.html) i got it working with:

```
Array.SetNum(Rows, false);

for (uint32 Row = 0; Row < Rows; Row++)
	Array[Row].Init(NULL, Columns);

```

The SetNum still sets the maximum size bigger than the Rows, so i would still like to know if there is a way to do something like Init

---

<div class="post-metadata">

**Author:** ![xlar8or](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xlar8or/32/710746_2.png) [@xlar8or](https://forums.unrealengine.com/u/xlar8or)\
**Post date:** [May 18, 2014, 9:57pm UTC](https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656/5 "2014-05-18T21:57:19Z")

</div>

After checking a 2D Array example on Lightmass.cpp i ended up doing this:

```
template <typename T>
void TArray2D<T>::Init(uint32 Rows, uint32 Columns, T Value)
{
	Array.Empty(Rows);
	Array.AddZeroed(Rows);

	for (uint32 Row = 0; Row < Rows; Row++)
		Array[Row].Init(Value, Columns);

	this->Rows = Rows;
	this->Columns = Columns;
}

```

And with this i have the array with the exact size i wanted.

---

<div class="post-metadata">

**Author:** ![xlar8or](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/xlar8or/32/710746_2.png) [@xlar8or](https://forums.unrealengine.com/u/xlar8or)\
**Post date:** [May 18, 2014, 10:00pm UTC](https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656/6 "2014-05-18T22:00:37Z")

</div>

Thank you for your answer Rama, looking at your code helped me reach to the solution that i commented, so i think it’s fair your answer is marked as accepted 🙂

---

<div class="post-metadata">

**Author:** ![frankiezafe](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/frankiezafe/32/732708_2.png) [@frankiezafe](https://forums.unrealengine.com/u/frankiezafe)\
**Post date:** [August 7, 2023, 3:53pm UTC](https://forums.unrealengine.com/t/why-is-my-multidimensional-array-implementation-crashing-on-init/283656/7 "2023-08-07T15:53:50Z")

</div>

Hello, I have a crash similar to yours in build only. It was worst (more frequent) when i was using `TSet` instead of `TArray`.  
Do you have any idea what might happens?
