Archived

This topic is now archived and is closed to further replies.

VersusReal

memory allocation in games....

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites