Download

Should I initialize unreal structs?

Hi! I am having lots of FStructs with all kind of data like


UPROPERTY()
TArray

UPROPERTY()
bool

and all works perfect so far. I just define them in header, and use on demand, with no additional initializing routines or constructors.

But somewhere deep within I hear voices ‘always initialize everything, or this might get filled up with garbage memory sometime and crash’

Am I being paranoid?

Yes, it is a good practice, for some reason in unreal if you left numeric values inside a struct without initialize, you will get funny behaviors, an int32 with 65535 instead of 0, stuff like that

Integers filling with some data is OK and expected behavior, but will it ever lead to some crashes, like TArray becoming corrupted? I am just being lazy, wonder if I can get away with it =)

You should always initialize everything. Don’t rely on Unreal to do it for you.

Non-primitives like TArray are always initialized automatically, but simple values like ints, floats or pointers might be filled with garbage.