Announcement

Collapse
No announcement yet.

FVector default constructor returns vectors filled with random values ?

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

    FVector default constructor returns vectors filled with random values ?

    When declaring vectors like this :

    Code:
    FVector TestVector;
    or like this :

    Code:
    FVector TestVector = FVector();
    The resulting vector is filled with random values. Is this the expected behaviour ?
    I would expect a 0 length vector.

    #2
    This is FVectors default constructor.

    Code:
    FORCEINLINE FVector::FVector()
    {}
    Because it's a standard C++ class the memory it occupies is not initialized before construction, and in this case the default constructor chooses not to initialize it either (for performance reasons I imagine), so the value of the vector is just garbage that was in that memory location previously. Use FVector TestVector(0.f) to initialize a zero vector.

    Comment


      #3
      Ok, thanks for your explanation The Beej. I'm a total noob with C++ and all those raw memory access "weird" behaviours are new to me.

      Seems like other classes like FRotator and FQuat act the same way, so I'll be more careful from now when calling default constructors.

      Comment

      Working...
      X