Download

Pointers in UnrealEngine C++

Hello, so basically I’ve been thinking, in general or pure c++ when we at first declare a pointer e.g. int* a; and we want to assign a value we dereference the pointer *a = 1;. so why is it that in Unreal we e.g. declare an int32* Value; and then assign it simply without dereferencing Value = 1; ? Sorry in advance if my question is weird maybe im a bit fuzzy loll <3

Are you sure that Value is set correctly and you are not changing the pointer to point to 0x01? Seems pretty stange to me that this works as int32 is defined as

typedef FPlatformTypes::int32 int32;

and FPlatformTypes::int32 is defined as

typedef signed int int32;

If you declare int* a; and then dereference by doing *a = 1; you will almost certainly get a crash because the value of a is either indeterminate or nullptr (if i has static storage duration). I don’t know where you saw int32* Value; Value = 1; but implicit int to int* conversions are not allowed in C++ so that won’t compile. If you know a specific address for sure you can set it with a cast like int32* Value = (int32*) 0x12345; but unless you really know what you are doing you are also asking for a crash when you dereference Value afterwards.

omg I’m so sorry that makes sense going through past projects I realized I’ve never had to even declare a pointer integer but I guess let’s say we’re declaring a simple component in a header file ‘USceneComponent* SpawPoint;’ and in the constructor we do ‘SpawnPoint = CreateDefaultSubobject(TEXT("SpawnPoint));’ so I guess this is slightly less stoopid than the ‘int32*’ example. I’m not very experienced with UEcpp yet so bare with me pls ty :smiling_face_with_three_hearts:

CreateDefaultSubobject creates a new object for you and returns a pointer to that object. There is a lot going on behind the scenes when you call that function but what it essentially boils down to is that the object needs a chunk of memory to live in and needs to be constructed and initialized, so to stay with your int32 example, it is a bit like calling int32* Value = new int32{1}; (oversimplification of the century).

1 Like