Overloading

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

Get Hands-On System Programming with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.