Struct, uninitialized local variable used if using pointer?

Hi I have this struct in it’s own .h file as I needed to include it in several of my classes.

#pragma once

#include "CoreMinimal.h"

struct FItemInfo
{
	UClass* ItemClass;
	int PickupID;
};

I try to return the struct with the variables filled in like this:

	FItemInfo* ReturnInfo;

		ReturnInfo->ItemClass = DropTable[ListIndex].ItemClass;
		ReturnInfo->PickupID = ListIndex;
		return ReturnInfo;

But get an error that the ReturnInfo is uninitialized. What am I missing? How would I manually initialize it before setting the variables?

Try UClass* ItemClass = nullptr;

1 Like

I still got the same error with that, however, if I did FItemInfo* ReturnInfo = nullptr; instead it compiles!

However, it’s giving me an error about trying to write to address 0 when assigning the values. So I need to initialize the struct somehow?

The problem is that you have only defined a pointer to a struct. You haven’t actually constructed an instance of that struct.

Simplest/safest solution is to avoid pointers altogether. Something like:

FItemInfo GetInfo()
{
    FItemInfo ReturnInfo;
    ReturnInfo->ItemClass = DropTable[ListIndex].ItemClass;
    ReturnInfo->PickupID = ListIndex;
    return ReturnInfo;
}

That should fix your issue. :slight_smile:

To explain the problem in more detail… your pointer was pointing to an undefined memory address. The compiler rightly warned you about this, because when you start writing to a random memory address, a crash is the best you can hope for! In the later example, when you set the pointer to null, it was pointing at a memory address of zero. This will crash when you start writing to it, as you observed.

The pseudocode I wrote above will allocate memory for the struct on the stack and then return a copy of it to the calling code. There are ways to avoid the unnecessary memory copy, but these days the compiler will probably optimize that for you. And for such a small struct I wouldn’t worry about it.

1 Like

That works perfectly thanks! I was indeed trying to refer to a struct instead of copying it.

For future reference, how would you create a pointer to blank struct like this?

You could dynamically allocate the struct using the new operator. For example:

FItemInfo* ReturnInfo = new FItemInfo();
ReturnInfo->ItemClass = DropTable[ListIndex].ItemClass;
ReturnInfo->PickupID = ListIndex;
return ReturnInfo;

However, you now need to take care to make sure the memory gets freed again once you’ve finished with it, otherwise you’ll have a memory leak.

To free the memory, you use the delete operator. For example:

// GetItemInfo dynamically allocates the Item Info (which i really don't recommend!)
FItemInfo* ItemInfo = GetItemInfo();

// Do some stuff with ItemInfo
...

// Release the memory
delete ItemInfo;
ItemInfo = nullptr;

Then you must not access the struct after that point.

Tbh I would strongly avoid using new/delete unless you are very confident in what you are doing. The potential for carnage is great! Unreal has some smart pointer classes that can help shield you from the lower-level memory management.

Another option is to construct the object outside the function and then pass it into your function (either by reference or by pointer) to be populated. For example:

FItemInfo ItemInfo;
GetItemInfo(ItemInfo);

void GetItemInfo(FItemInfo& ItemInfo)
{
ItemInfo.ItemClass = DropTable[ListIndex].ItemClass;
ItemInfo.PickupID = ListIndex;
}

1 Like

And if you want to get a pointer to an existing struct object, you can get it as follows:

FItemInfo ItemInfo;
FItemInfo* ItemInfoPtr = &ItemInfo;

But again I recommend only using pointers if you absolutely need to. Pointers have the potential to be null, or pointing at garbage. So if you’re not confident of the contents and lifetime of the memory being pointed at, then you’re going to have a bad time. :slight_smile:

1 Like