Introducing local buffer optimization
The least amount of work a program can do to accomplish a certain task is no work at all. Free stuff is great. Similarly, the fastest way to allocate and deallocate memory is this - don’t. Local buffer optimization is a way to get something for nothing; in this case, to get some memory for no additional computing cost.
The main idea
To understand local buffer optimization, you have to remember that memory allocations do not happen in isolation. Usually, if a small amount of memory is needed, the allocated memory is used as a part of some data structure. For example, let’s consider a very simple character string:
// Example 04 class simple_string { public: simple_string() = default; explicit simple_string(const char* s) : s_(strdup(s)) {} simple_string(const simple_string& s) : s_(strdup(s.s_)) {} simple_string& operator=(const char...