I'm wondering what value a bit-field class member variable will have if it is not explicitly initialized.
Using an example from unreal engine 4.27:
//member variable of UPrimitiveComponent
//there are other uint8 bitfields declared above and below this
UPROPERTY(...some UE4 macro stuff...)
uint8 bCastHiddenShadow:1;
It is not explicitly initialized in the header, the constructor member initializer list or the constructor body. Yet it seems to get initialized to 0 fine. For a non-bitfield variable I think this would be undefined behaviour, but I assume this isn't the case here? Or does the value depend on the initialization of nearby packed bit-field variables (some of which are explicitly initialized)?
(Or, not sure if the UPROPERTY macro is somehow doing some magic to initialize it? Apologies, couldn't find a full definition of the macro anywhere)
Dug a little bit deeper, so I'll try to answer this myself. I believe that for a normal C++ class it would be undefined behaviour as I cannot find any info suggesting otherwise for bit-fields specifically.
For the UE4 example, most objects in the engine including the cited UPrimitiveComponent
example are derived from UObject
, and I did find deep in the documentation that these are automatically zero initialized:
Automatic Property Initialization UObjects are automatically zeroed on initialization, before the constructor is called. This happens for the whole class, UProperties and native members alike. Members can subsequently be initialized with custom values in the class constructor.
In the code this will happen in StaticAllocateObject
in UObjectGlobals.cpp
, the memory is earlier malloc
'd and then FMemory::Memzero
is called which ultimately uses memset
to zero the memory