C++ Spawning new objects in a game engine

Started by
6 comments, last by N0T0R10US 20 years, 9 months ago
I've developed a simple game engine, deriving all game objects from a simple base class and storing pointers to them in a linked list which I process in the main game loop. What's the best approach to spawning short lived objects. Will I take a performance hit if I add them to the linked list only to remove them a couple of cycles later ? Or do I reserve space for the maximum number of objects in one go and live with the overhead ? Sorry, this a pretty generic q. Is there a rule of thumb, irrespective of the game genre ?
Advertisement
During runtime you''d want to be using new as little as possible, so reserving space for the maximum amount is probably the best way to go.
---------------------Ryan Bujnowicz[ vangelis ]
It''s always learning a few tricks from the masters (id etc.) - have a look at the source code for Doom, Quake and the like. They tend to allocate a big chunk of memory when the game initialises and use their own memory management to handle allocations dynamically.

I seem to remember seeing something in the source code for Quake which does something to make sure that Windows doesn''t page the memory out to disk (obviously making things very slow!) - can''t remember exactly what it was but like I said, have a look at the source code and pick up some tips.
I''m a little too lazy to trawl throught the source code for Quake, but I like the idea of a custom memory management module.

I''ve read recently that if paging to disk wasn''t enough to worry about, that there are performance wins to be made by programming around the memory cache.

Just want to make sure I get off on the right foot. They''ll be plenty of time to tune near the end of my project.

Thanks for the advice so far
Depends on how many objects to create. If you use a recycling array that reservs memory at start, you won''t have any trouble with that later, but I usually use some sort of stl container.

list, queue, map or vector depending on what kind of things to be managed. This is not the most optimized way, but it''s pretty fast and very easy! =)
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
Thanks for the all the advice...

I''m gonna go the custom memory management route.

I''ll keep you posted
I just wrote something similar for the game my company is working on, so let me know if you have any questions.

-- Steve --
-- Steve --
Cheers Steve.

Did you use STL containers ? or was it completely custom ?

This topic is closed to new replies.

Advertisement