What I Learned from Scott Meyers' Effective C++, Part 8
Chapters 8 & 9: Customizing new and delete & Miscellany
In my previous chapter, we discussed Templates and Generic Programming. In this chapter, we'll talk about what I learned in chapters 8 and 9 of the book, items 49 to 55.
Item 49: Understand the behavior of the new-handler
You can use set_new_handler to, you guessed it, set a new handler to be called when a memory allocation request is failed. Nothrow new only applies to memory allocation, the consisting expression may still throw.
Item 50: Understand when it makes sense to replace new and delete
Library standard new and delete operators are general purpose tools. If you understand your program's memory usage patterns and dynamics you are likely to implement a more efficient new and delete operator yourself. However, there are some details that need addressing if you'd like to do this. You can write a custom new, delete operator to detect usage errors and statistics or improve memory allocation and deallocation speeds or reduce space overhead of the default systems or cluster certain objects together or just implement unconventional behavior that is not implemented in compiler-provided versions.
Item 51: Adhere to convention when writing new and delete
Writing your own custom new and delete handlers can be tricky. It's important to study the conventions in the compiler-implemented versions and stick to them to avoid any undefined behavior and crashes.
Item 52: Write placement delete if you write placement new
When a memory allocation operation fails before it was able to return the memory address to its caller it should be appropriately cleaned up. However, if you're writing custom new and delete operators you have to ensure that the corresponding new and deletes are written properly otherwise the system may find it difficult to understand which versions to use and leak memory.
Item 53: Pay attention to compiler warnings
The compiler is mostly your friend, don't ignore it and understand each warning. Also, don't be dependent on compiler warnings as different compilers warn differently.
Item 54: Familiarize yourself with the standard library
The standard library is also your friend, a multitude of data structures, algorithms and tools matured over years are ready for your use, don't underestimate it.
Item 55: Familiarize yourself with Boost
Not much to explain here, boost plays an essential role in developing standard C++ libraries and functionalities.
Thanks for joining me on this journey with this book. This was it for now. Hope this intrigued you to go and get a copy of Scott's book.