Do variables use up memory if they are null?

Lets say my enemy class has a ton of variables linking to other classes. For example:



UPROPERTY(EditAnywhere) AWeapon* weapon;
UPROPERTY(EditAnywhere) AJumpThing* jumpthing;
UPROPERTY(EditAnywhere) TArray<APowerups*> powerups;
etc...


If most of those will never be used on most enemies, is it still okay to leave them in the class as nullptr? Or would it be more efficient to make a second class strictly for enemies that use them?

Basically I’m not sure if they reserve memory just by existing, or if memory is only used when they actually contain objects.

A Pointer is just an address in memory, usually 4 bytes (32 bit) or 8 bytes (64 bit)
So if these values are null they do occupy memory (the size of the address).

This is negligible though.
Whats important is what they are pointing at.
So if the values are not null, the memory of this particular object is still the same, but the overall used memory is larger (because there are other objects which these pointers are pointing at)


This is image is heavily simplified. there is some more overhead for the Memory management and reflection stuff of unreal.

But you should get the idea.

The size of your object is still the same regardless whether the pointers are null or not

Ah I see, the size of the pointers themselves are very small.

If I understand correctly, I can have loads of enemies in game, all with a bunch of nulled pointers and it shouldn’t affect memory/performance.

Then I can just have the pointers be not-null on certain enemies where needed. Thanks! :slight_smile: