Often when programming systems, the default allocation scheme provided by C++ is undesirable. Examples include (but are not limited to):
- Custom memory layouts
- Fragmentation
- Performance optimizations
- Debugging and statistics
One way to overcome these issues is to leverage C++ allocators, a complex topic that will be discussed in Chapter 9, A Hands-On Approach to Allocators. Another, more heavy-handed, way to achieve this is to leverage the new() and delete() operators' user-defined overloads:
#include <iostream>void *operator new (std::size_t count){ // WARNING: Do not use std::cout here return malloc(count);}void operator delete (void *ptr){ // WARNING: Do not use std::cout here return free(ptr);}int main(){ auto ptr = new int; ...