Memory Management: Overloading Operator new and delete 1
Explore how to overload operator new and delete in Modern C++ to manage dynamic memory effectively. Understand how to track allocations and deallocations, detect memory leaks, and analyze their addresses. This lesson helps you gain skills to improve memory handling in safety-critical and resource-limited embedded applications.
It happens quite often that a C++ application allocates memory but does not deallocate it. This is the job for the operators new and delete. Due to both features, you can explicitly manage the memory management of an application.
Occasionally, we must verify that an application has correctly released its memory. In particular, for programs running for long periods of time, it is a challenge to allocate and deallocate memory from a memory management perspective. Of course, the automatic release of the memory during the shutdown of the program is not an option.
The Baseline
As a baseline for our analysis, we use a simple program that often allocates and deallocates memory.
The key question is as follow:, is there a corresponding delete to each new call?
Operator new
C++ offers the operator new in four variations:
void* operator new (std::size_t count );
void* operator new[](std::size_t count );
void* operator new (std::size_t count, const std::nothrow_t& tag);
void* operator new[](std::size_t count, const std::nothrow_t& tag);
The first two variations will throw ...