xlar8or
(xlar8or)
May 18, 2014, 2:39pm
1
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
xlar8or
(xlar8or)
May 18, 2014, 8:28pm
2
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.
Rama
(Rama)
May 18, 2014, 8:31pm
3
#2D TArrays: Complete Code Sample
I posted a full code sample on 2D TArray’s here
#Add Uninitialized
Take careful note of the use of AddUninitialized at the end of my example above.
Rama
xlar8or
(xlar8or)
May 18, 2014, 8:44pm
4
Based on this link 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
xlar8or
(xlar8or)
May 18, 2014, 9:57pm
5
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.
1 Like
xlar8or
(xlar8or)
May 18, 2014, 10:00pm
6
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
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?