Home > Mobile >  should I check pointers in such situations?
should I check pointers in such situations?

Time:03-27

I'm new to programming and game development in general every time I read and hear that pointers are a nightmare I want to ask if it is necessary to check pointers in such cases as shown below?

// create a component of a certain type and return a pointer to data of this type
StaticMeshCompt = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
if (StaticMeshCompt)
{
   // further work with the component
}

CodePudding user response:

No, there's no need to check the pointer. The cases where CreateDefaultSubobject could return nullptr are :

  • The class passed is nullptr (It's guaranteed to be valid for any UObject).
  • The class has the abstract flag (Not the case for UStaticMeshComponent).
  • Allocation itself fails due to lack of free memory (At that point, you've got other problems).

CodePudding user response:

As David G and ChrisMM said, it is necessary to check the pointer if the CreateDefaultSubobject function has a possibility of failing or returning a null pointer. If the function is known to always return a valid object, checking may not be necessary in that case.

  • Related