Variable declaration or new?

I can seldom see people use “new” keyword to create variables inside functions. Instead, the direct declaration is used. If there are a lot of variables created at the same time, then will the stack soon be filled up and no more free space is left? If this happens, the program should report the error, but I never see such. Is there a checking mechanism inside Unreal 4? Tons of functions are running at the same time, so countless number of variables should be created and exist in the stack. Why don’t we just use new to create variables in the heap?

Thanks,

wcl1993

Unreal implements its own Garbage Collection (for most Unreal-specific objects) so I assume this is all managed perfectly well internally. I don’t have any idea what it does underneath I’m afraid - that’s beyond my expertise.

So I suppose there’s really no need use “new” or destroy objects manually unless using non-unreal-specific objects, where standard C++ behaviours apply.

And if you need to allocate memory with the new keyword use the unreal engines smart pointers instead. They are easy to handle and eliminate the risk of memory leaks.

OK. Since most of my actors should derive from UObject class, it should work for them. But if there happens to be one or two classes that is not derived from UObject, then maybe I need to manage the memory by myself?

Yup. The short of it is that the Unreal garbage collection system relies on reflection, so it knows only about unreal objects.