-
Book Overview & Buying
-
Table Of Contents
C++ Memory Management
By :
We just overloaded memory allocation operators, blatantly lied our way through the protections of the type system, performed potentially dangerous operations that risked leading to misaligned objects, and saw how to avoid this pitfall. That was an interesting adventure indeed, but the astute reader that you are is probably wondering about the cost of this trick, particularly in terms of how much memory it consumes.
With our “allocate more than requested and hide n at the beginning” approach, each allocation consumes sizeof(std::max_align_t) bytes more than needed by client code. If our code allocates large objects, that cost might be minor, but if we allocate smaller objects, this overhead can be unreasonable and dominate the memory consumption of our entire program.
Remember from Chapter 7 that C++14 made it possible to provide an overload of operator delete() that accepts the size of the just-destroyed object...