Allocate UObjects contiguously?

Hi all,

Just wondering if there’s some method I’ve missed to create/allocate n UObjects in a contiguous block?

E.g. if I want 100 - or even 1000 - UObjects, and thus call NewObject(…) 100 or 1000 individual times, it looks like they’ll be fragmented throughout the heap. Is this the case, or am I misunderstanding the UObject allocation process?

Would be interesting trying to write a function to do this into the engine.


Use a TArray to hold your objects; it allocates memory in a contiguous block.

EDIT: That isn’t really true is it; the object pointers will be contiguous but the object memory won’t be. It’s kind of hard to think of a non-trivial object that won’t be given it’s probably composed of other sub-objects who’s memory will be “fragmented”.

What is your requirement to have the objects in contiguous memory as I’ve never had this requirement?

If you’re iterating over the objects it makes a huge difference to cache coherency, and therefore performance. You could use an array of structs for your interable data and hold an index into that data from each object. Then when you want to iterate you can iterate over the structs instead.

Yepp, it has its advantages. I’m not looking to have any components or anything on objects, but just data straight on the stack.

I could definitely have separate structs etc for data, but I’m already working in a non-UE4 simulation. I just wanted to tag the base objects as UObjects so I could easily get some ‘free’ scripting integration by using the Blueprints. Not being able to allocate 1000+ objects contiguously is an issue, though.