Memory Management: Overloading Operator new and delete 2
Explore techniques to enhance memory management in embedded programming by overloading the new and delete operators. Understand how macros capture file and line details for debugging and how to implement dynamic memory tracking at runtime with std::vector. This lesson helps you identify memory leaks and optimize resource handling in modern C++ code.
We'll cover the following...
What were the not-so-nice properties of the previous lesson?
Firstly, we only get a hint of which memory was lost. Secondly, we had to prepare the whole bookkeeping of memory management at compile time. In this lesson, we aim to overcome these shortcomings.
Who is the Bad Guy?
Special tasks call for special strategies. We must use a small macro for debugging purposes.
Let’s take a look at this macro. #define new new(__FILE__, __LINE__)
The macro causes each new call to be mapped onto the overloaded new call. This overloaded new call also receives the name of the file and the line number respectively. That is exactly the information we need to solve this problem.
So, what will happen if we use the macro in line 6?
The preprocessor substitutes all new ...