Announcement

Collapse
No announcement yet.

Do variables use up memory if they are null?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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:
    Code:
    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.

    #2
    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)

    Click image for larger version  Name:	Unbenannt.jpg Views:	1 Size:	143.2 KB ID:	1621334
    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
    Last edited by Raildex_; 05-19-2019, 04:17 AM.

    Comment


      #3
      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!

      Comment

      Working...
      X