Download

Question regarding to pointers (nullptr) and memory allocation for unreal types.

[SIZE=16px][COLOR=#000000]hi, do i´m forced to declare all pointers as nullptr if i´m not initializing it ?..
i remember read some places coding:



class AActor * anActor;


but in other places:



class AActor * anActor = nullptr;

[/COLOR]



 

[/SIZE]

[SIZE=16px][COLOR=#000000]i noticed sometimes if i don´t put nullptr, Unreal crashes, but still don´t get why in some is needed and why in other not…[/COLOR][/SIZE]

[SIZE=16px][COLOR=#000000]theorically this should crash, but does not…:



//header file
class UStaticMeshComponent* anActor;

//cpp constructor
anActor = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));


anActor is not requestion dinamic memory allocation, so, why compiler accepts it?[/COLOR][/SIZE]
[SIZE=16px][COLOR=#000000]or maybe the class CreateDefaultSubobject<>() template is allocating automatically memory for me ?[/COLOR][/SIZE]

I believe the default value for any pointer is nullptr, putting the “= nullptr” part is just good practice.
The crashes mostly occur when you dereference a null pointer. (Accessing to the variable the pointer points to).

The CreateDefaultSubobject function creates an object and returns a pointer to it’s address, there is no dereferencing here

hello again?.. yeah, i got it…

what ?? I’m not following you there