filmov
tv
TArray vs std::vector - Unreal C++ - UE C++ Tutorial

Показать описание
std::vector and TArray are similar, but they're also very different.
TArray is built for games, and it does raw memory copies during buffer resizes.
std::vector is built for general purpose c++ programming. It invokes copy, move, and destructors during buffer resizes.
This means that TArray ultimately is using less cycles on a buffer resize, making it potentially faster.
std::vector also does more buffer resizes for the same number of elements added.
However, this means that certain patterns in c++ might break with TArray.
For example, if you use references or pointers internally to your class, and you use it by value in a tarray, you may have dangling pointers.
As the TArray will copy and paste the memory into the new buffer, which will have a different memory address.
However, in practice within unreal this is not common. Because most pointers leave on the heap, so it is fine to do shallow copies.
But it is important to be aware of this behavior, so you don't accedentially write dangerous code.
I recommend using TArray over std::vector while in the engine.
It is fully supported by the scripting system, and heavily embedded as part of the Unreal ecosystem.
0:00 TArray and std::vector are similar, but difference
0:21 Custom struct that reports constructor operations
1:01 What happens when we dynamically add and remove elements with TArray
2:15 What happens when we dynamically add and remove elements with std::vector
5:10 Comparing std::vector and TArray in terms of constructors called
5:45 Testing what happens during a TArray buffer resize by adding a 5th element.
7:15 Summary
8:15 Outro
TArray is built for games, and it does raw memory copies during buffer resizes.
std::vector is built for general purpose c++ programming. It invokes copy, move, and destructors during buffer resizes.
This means that TArray ultimately is using less cycles on a buffer resize, making it potentially faster.
std::vector also does more buffer resizes for the same number of elements added.
However, this means that certain patterns in c++ might break with TArray.
For example, if you use references or pointers internally to your class, and you use it by value in a tarray, you may have dangling pointers.
As the TArray will copy and paste the memory into the new buffer, which will have a different memory address.
However, in practice within unreal this is not common. Because most pointers leave on the heap, so it is fine to do shallow copies.
But it is important to be aware of this behavior, so you don't accedentially write dangerous code.
I recommend using TArray over std::vector while in the engine.
It is fully supported by the scripting system, and heavily embedded as part of the Unreal ecosystem.
0:00 TArray and std::vector are similar, but difference
0:21 Custom struct that reports constructor operations
1:01 What happens when we dynamically add and remove elements with TArray
2:15 What happens when we dynamically add and remove elements with std::vector
5:10 Comparing std::vector and TArray in terms of constructors called
5:45 Testing what happens during a TArray buffer resize by adding a 5th element.
7:15 Summary
8:15 Outro
Комментарии