memory allocation in games....

Started by
3 comments, last by VersusReal 21 years, 8 months ago
How should memory allocation be done in games? Do you use new and delete or malloc and free? Or does it matter... Could you post some samples of the best one please? I''m having problems with it... Thanks! Open the hushed casket...
Open the hushed casket...
Advertisement
It''d take too long to describe it, but there''s a pretty good memory management system described in the book "Game Programming Gems" (The first one in the series). Basically you allocate all the memory you''re going to use up front, then you use back and forth allocations, like putting all the graphics at the bottom and all the sound on the top....or whatever.

I know, not a very good explanation, but like I say, it''d take too long to describe it here. The crux of what makes it good is that you define memory ONCE and then you do your best internally to allocate memory within that set of memory as non-fragmented as possible.

-John
- John
Memory allocated for graphics and sound shouldn''t be all that fragmented anyway - I''d use new and delete if I were you, then you have an unlimited about available to you (well, you only have 4Tb of address space on a 32-bit machine). malloc and free are very similar, I''m sure someone else can explain what little differences there are better than I can. Avoid using them realtime where possible, though it isn''t the end of the world if you do, and leave the rest to the OS / compiler.
Differences:
New calls object constructor after creation, malloc doesn''t.
Dellete calls object destructor before deletion, free doesn''t.


Homepage.
quote:Original post by Kamil
Differences:
New calls object constructor after creation, malloc doesn''t.
Dellete calls object destructor before deletion, free doesn''t.


Homepage.


New and delete also don''t need sizeof. They also return a pointer of the appropriate type.

You do not need to be using C++ to use new and delete, so they are recommended to be used as the syntax is also easier.



---
Make it work.
Make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement