Game Development Reference
In-Depth Information
Summary
This chapter has given you a very simple introduction to the C++ memory management model.
You've seen that your programs will use static memory, stack memory, and heap memory to store
the objects and data to be used by your games.
Static memory and stack memory are handled automatically by the compiler, and you've already
been using these types of memory throughout this topic without having to do anything in particular.
Heap memory has higher management overhead, as it requires that you also free the memory
once you have finished using it. You've seen that the STL provides the unique_ptr and shared_ptr
templates to help automatically manage dynamic memory allocations. Finally, you were introduced
to a simple memory manager. This memory manager would be unsuitable for production code, but
it does provide you with an overview of how memory is allocated from a heap and how you can
overload the global new and delete methods to hook in your own memory manager.
Extending this memory manager to be fully featured would involve adding support for reallocating
freed blocks, defragmenting contiguous free blocks in the heap, and eventually ensuring the
allocation system is thread safe. Modern games also tend to create multiple heaps to serve different
purposes. It's not uncommon for games to create memory allocators to handle mesh data, textures,
audio, and online systems. There could also be thread-safe allocators and allocators that are not
thread safe that can be used in situations where memory accesses will not be made by more than
one thread. Complex memory management systems also have small block allocators to handle
memory requests below a certain size to help alleviate memory fragmentation, which could be
caused by frequent small allocations made by the STL for string storage, and so on. As you can see,
the topic of memory management in modern games is a far more complex problem than can be
covered in this chapter alone.
 
Search WWH ::




Custom Search