Memory

Every day spent coding is different thanks to the NULL pointer.


There are three types of memory usage when using C++. They are static, stack and heap.

Static

Static memory is the ones allocated to the program when it start to store the code and the global structures that it use.

Stack

Used to store function return addresses, functions arguments and variables declared local in a functions. The stack has a limited size and if the big local variables are created or to much recursion there will be a stack overflow.

    • To Change the stack size in Visual Studio use /F option.

    • The details about who puts what on stack and when is selected by the calling conventions of functions. When calling a external functions you might need to select correct convention so the call works.

Heap

In C++ the memory from the heap is allocated with new and when it is not needed anymore it is removed with delete.

new / delete

Overloading New / Delete

It is possible to overload new and delete so custom actions is taken when you try to get memory for something.

malloc / free

calloc / realloc

Heap Memory Management

Memory Manager

Memory Pool

With a memory pool you keep a a maximum number of objects you need allocated all the time. Ex if you make a space shooter you have 100 laser bolt objects allocated in a array. When you need one you can simple get a pointer to one not in use and when the bolt is destroyed you can flag it as not in use and let it be. That way one does not have to new and delete the memory for them, something that can get expansive when the player spray the world with his laser gatling gun.


Memory Alignment


Reference

Custom memory allocators - 2021


Dev: It’s all about memory - 2020 – part 1, part 2 and part 3.

Dev: Tracking Memory Usage - 2020 - part 1 and part 2


Writing Cache-Friendly C++ - 2018


C++ Core Guidelines: Rules for Allocating and Deallocating- 2017

Virtual Memory Tricks - 2017


Building a Low-Fragmentation Memory System for 64-bit Games - 2016

Memory Allocators: Table of Contents - 2016

Operator New and Delete - Unnecessary Conditions - 2016


Allocation Adventures - 2015: Part 1 , Part 2

Virtual memory explained - 2015

A memory allocator interface - 2015

Caches everywhere - 2015

gin - 2015

Freelist Concept - 2015

Memory layout of multi-dimensional arrays - 2015

C++ STL container deficiencies - 2015


Memory Management: Pool Allocator - 2014

Memory Management: Slab allocator - 2014

Memory Management: the SLUB allocator - 2014

Memory Management - 2014

A queue of page faults- 2014

Memory Management - 2014


C++: Custom memory allocation - 2013

Memory System - 2013

Memory allocation strategies: a growing stack-like (LIFO) allocator - 2013


Memory Management Strategies - 2012

Memory allocation strategies - 2012

Garbage collection thoughts - 2012

Memory allocation strategies interlude: virtual memory - 2012

Memory allocation strategies: a pool allocator - 2012

Memory allocation strategies: a stack-like (LIFO) allocator - 2012

Memory allocation strategies: a linear allocator - 2012


Memory system: Part 1, Part 2, Part 3 and Part 4. - 2011

Introduction - 2011

Allocations Tracking - 2011

Memory Allocators Design - 2011

Allocators Overhead, Waste and Fragmentation - 2011

Virtual Addressing 101 - 2011


Scope Stack Allocation - 2010

Start Pre-allocating And Stop Worrying - 2010


Debugging Memory Corruption in Game Development - 2008


Virtual Memory System on Nintendo Gamecube - 2007

What Every Programmer Should Know About Memory - 2007


Alternatives to malloc and new

Memory Pool

Alternatives to malloc and new