Download

Do native member variables that aren't UPROPERTY need to be initialized?

In C++ if something is a UProperty I always assume it will be initialized to some default, usually 0 for numbers, for example.

Normally in C++ if you don’t initialize a member variable it’ll have garbage of whatever is in memory. I noticed, however, in Unreal, I have some C++ only member vars in some of my UObjects that I forgot to explicitly initialize, and I haven’t had any issues.

Does Unreal know to somehow zero out member vars when it’s a UObject?

C++ still works like C++ if no UPROPERTY is specified.

  1. Non-basic types get default-initialized and it’s ok
  2. Not initializing basic types is UB. They might not get zeroed in packaged release build, you can try it there to see what will happen, but for what?

You should always initialize your variables, or something else will. You may get lucky and UObjects get memset to 0 for their POD types somewhere, but I wouldn’t take that chance. Also, remember that uninitialized variables will be set to 0 in DEBUG, but random memory in SHIPPING / RELEASE.

Yeah +1 for initializing anyway, it’s a good habit to get into.

I’m not 100% sure but I seem to recall somebody saying that the memory footprint of a UObject will be mem-zeroed first, before being reinitialized with the class-default object, so by sheer luck it might be that everything is zero initially - but it’s not a chance worth taking, and that still doesn’t garauntee the type will be ‘safe’.