{"id":5108,"date":"2016-12-30T17:47:09","date_gmt":"2016-12-30T17:47:09","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/overloading-operator-new-and-delete\/"},"modified":"2023-06-26T12:29:18","modified_gmt":"2023-06-26T12:29:18","slug":"overloading-operator-new-and-delete","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/overloading-operator-new-and-delete\/","title":{"rendered":"Overloading Operator new and delete 1"},"content":{"rendered":"<p>It happens quite too often that a C++ application allocates memory but doesn&#8217;t deallocate it. This is the job of the operator to new and delete. Thanks to them both, you can explicitly manage the memory management of an application.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>Occasionally, I had to verify that a customer&#8217;s application correctly releases its memory. In particular, programs running for an extended period and often allocating and deallocating memory are challenging from the memory management perspective. Of course, the automatic memory release during the program&#8217;s shutdown is not an option.<\/p>\n<h2>The baseline<\/h2>\n<p>As a baseline of my analysis, I use a simple program that often allocates and deallocates memory. In production, the code is bigger. That does not affect my strategy.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ overloadOperatorNewAndDelete.cpp<\/span>\r\n\r\n<span style=\"color: #008000;\">\/\/ #include \"myNew.hpp\"<\/span>\r\n<span style=\"color: #008000;\">\/\/ #include \"myNew2.hpp\"<\/span>\r\n<span style=\"color: #008000;\">\/\/ #include \"myNew3.hpp\"<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">MyClass<\/span>{\r\n  <span style=\"color: #2b91af;\">float<\/span>* p= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">float<\/span>[100];\r\n};\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">MyClass2<\/span>{\r\n  <span style=\"color: #2b91af;\">int<\/span> five= 5;\r\n  std::string s= <span style=\"color: #a31515;\">\"hello\"<\/span>;\r\n};\r\n\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n    \r\n    <span style=\"color: #2b91af;\">int<\/span>* myInt= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>(1998);\r\n    <span style=\"color: #2b91af;\">double<\/span>* myDouble= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">double<\/span>(3.14);\r\n    <span style=\"color: #2b91af;\">double<\/span>* myDoubleArray= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">double<\/span>[2]{1.1,1.2};\r\n    \r\n    MyClass* myClass= <span style=\"color: #0000ff;\">new<\/span> MyClass;\r\n    MyClass2* myClass2= <span style=\"color: #0000ff;\">new<\/span> MyClass2;\r\n    \r\n    <span style=\"color: #0000ff;\">delete<\/span> myDouble;\r\n    <span style=\"color: #0000ff;\">delete<\/span> [] myDoubleArray;\r\n    <span style=\"color: #0000ff;\">delete<\/span> myClass;\r\n    <span style=\"color: #0000ff;\">delete<\/span> myClass2;\r\n    \r\n<span style=\"color: #008000;\">\/\/  getInfo();<\/span>\r\n    \r\n}<span id=\"transmark\"><\/span>\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The key question is. Is there a corresponding <span style=\"font-family: courier new,courier;\">delete<\/span> for each <span style=\"font-family: courier new,courier;\">new<\/span> call?<\/p>\n<\/p>\n<h2>Too careless<\/h2>\n<p>The question is quite simple to answer by overloading the global operator new and delete. I count for each operator, how often it was called.<\/p>\n<h3>Operator new<\/h3>\n<p>C++ offers the operator new in four variations.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #2b91af;\">void<\/span>* <span style=\"color: #0000ff;\">operator<\/span> new  (std::<span style=\"color: #2b91af;\">size_t<\/span> count );\r\n<span style=\"color: #2b91af;\">void<\/span>* <span style=\"color: #0000ff;\">operator<\/span> <span style=\"color: #0000ff;\">new<\/span>[](std::<span style=\"color: #2b91af;\">size_t<\/span> count );\r\n<span style=\"color: #2b91af;\">void<\/span>* <span style=\"color: #0000ff;\">operator<\/span> new  (std::<span style=\"color: #2b91af;\">size_t<\/span> count, <span style=\"color: #0000ff;\">const<\/span> std::<span style=\"color: #2b91af;\">nothrow_t<\/span>&amp; tag);\r\n<span style=\"color: #2b91af;\">void<\/span>* <span style=\"color: #0000ff;\">operator<\/span> <span style=\"color: #0000ff;\">new<\/span>[](std::<span style=\"color: #2b91af;\">size_t<\/span> count, <span style=\"color: #0000ff;\">const<\/span> std::<span style=\"color: #2b91af;\">nothrow_t<\/span>&amp; tag);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The first two variations will throw a <span style=\"font-family: courier new,courier;\">std::bad_alloc<\/span> exception if they can not provide the memory. The last two variations return a null pointer. It&#8217;s quite convenient that is sufficient to overload only version 1 because versions 2 &#8211; 4 use the version 1: <span style=\"font-family: courier new,courier;\">void* operator new(std::size_t count)<\/span>. This statement also holds for variants 2 and 4, designed for C arrays. You can read the details of the global operator new here: <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/memory\/new\/operator_new\">cppreference.com.<\/a><\/p>\n<p>The statements also hold for <span style=\"font-family: courier new,courier;\"><span style=\"font-family: courier new,courier;\">operator delete<\/span>.<\/span><span style=\"font-family: courier new,courier;\"><br \/><\/span><\/p>\n<h3>Operator delete<\/h3>\n<p>&nbsp;C++ offers six variations for operator delete.<span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> delete  (<span style=\"color: #2b91af;\">void<\/span>* ptr); \t\r\n<span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> <span style=\"color: #0000ff;\">delete<\/span>[](<span style=\"color: #2b91af;\">void<\/span>* ptr);\r\n<span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> delete  (<span style=\"color: #2b91af;\">void<\/span>* ptr, <span style=\"color: #0000ff;\">const<\/span> std::<span style=\"color: #2b91af;\">nothrow_t<\/span>&amp; tag);\r\n<span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> <span style=\"color: #0000ff;\">delete<\/span>[](<span style=\"color: #2b91af;\">void<\/span>* ptr, <span style=\"color: #0000ff;\">const<\/span> std::<span style=\"color: #2b91af;\">nothrow_t<\/span>&amp; tag);\r\n<span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> delete  (<span style=\"color: #2b91af;\">void<\/span>* ptr, std::<span style=\"color: #2b91af;\">size_t<\/span> sz);\r\n<span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> <span style=\"color: #0000ff;\">delete<\/span>[](<span style=\"color: #2b91af;\">void<\/span>* ptr, std::<span style=\"color: #2b91af;\">size_t<\/span> sz);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>According to <span style=\"font-family: courier new,courier;\">operator new<\/span>, it is sufficient to overload for <span style=\"font-family: courier new,courier;\">operator delete<\/span> the first variant because the remaining five use <span style=\"font-family: courier new,courier;\">void operator delete(void* ptr) <\/span>as a fallback. Only a word about the two last versions of <span style=\"font-family: courier new,courier;\">operator delete.<\/span> You have in this version the length of the memory block in the variable <span style=\"font-family: courier new,courier;\">sz<\/span>&nbsp; to your disposal. Read the details here at <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/memory\/new\/operator_delete\">cppreference.com<\/a>.<\/p>\n<p>This time I use the program <span style=\"font-family: courier new,courier;\">overloadOperatorNewAndDelete.cpp<\/span> the header <span style=\"font-family: courier new,courier;\">myNew.hpp<\/span> (line 3). The same holds for line 34. Here I invoke the function <span style=\"font-family: courier new,courier;\">getInfo<\/span> to get information about my memory management.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ myNew.hpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#ifndef MY_NEW<\/span>\r\n<span style=\"color: #0000ff;\">#define MY_NEW<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;cstdlib&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;new&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">static<\/span> std::<span style=\"color: #2b91af;\">size_t<\/span> alloc{0};\r\n<span style=\"color: #0000ff;\">static<\/span> std::<span style=\"color: #2b91af;\">size_t<\/span> dealloc{0};\r\n\r\n<span style=\"color: #2b91af;\">void<\/span>* <span style=\"color: #0000ff;\">operator<\/span> new(std::<span style=\"color: #2b91af;\">size_t<\/span> sz){\r\n    alloc+= 1;\r\n    <span style=\"color: #0000ff;\">return<\/span> std::malloc(sz);\r\n}\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> <span style=\"color: #0000ff;\">delete<\/span>(<span style=\"color: #2b91af;\">void<\/span>* ptr) noexcept{\r\n    dealloc+= 1;\r\n    std::free(ptr);\r\n}\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> getInfo(){\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n \r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Number of allocations: \"<\/span> &lt;&lt; alloc &lt;&lt; std::endl;\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Number of deallocations: \"<\/span> &lt;&lt; dealloc &lt;&lt; std::endl;\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n}\r\n\r\n<span style=\"color: #0000ff;\">#endif <\/span><span style=\"color: #008000;\">\/\/ MY_NEW<\/span>\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I create in the file two static variables <span style=\"font-family: courier new,courier;\">alloc<\/span> and <span style=\"font-family: courier new,courier;\">dealloc<\/span> (lines 10 and 11). They keep track of how often I used the overloaded <span style=\"font-family: courier new,courier;\">operator new<\/span> (line 13) and <span style=\"font-family: courier new,courier;\">operator delete<\/span> (line 18). I delegate in the functions the memory allocation to<span style=\"font-family: courier new,courier;\"> std::malloc<\/span> and the memory deallocation to<span style=\"font-family: courier new,courier;\"> std::free<\/span>. The function <span style=\"font-family: courier new,courier;\">getInfo<\/span> (lines 23 &#8211; 31) provides me with the numbers and displays them.<\/p>\n<p>The question is. Have I cleaned everything clean?<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5105\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew.png\" alt=\"myNew\" style=\"margin: 15px;\" width=\"364\" height=\"192\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew.png 364w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew-300x158.png 300w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><\/p>\n<p>Of course, not. That was the intention of this and the following post. Now I know that I have leaks. Maybe it helps to determine the addresses of the objects which I have forgotten to clean up.<\/p>\n<h2>Addresses of the memory leaks.<\/h2>\n<p>So, I have to put more cleverness into the header <span style=\"font-family: courier new,courier;\">myNew2.hpp.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ myNew2.hpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#ifndef MY_NEW2<\/span>\r\n<span style=\"color: #0000ff;\">#define MY_NEW2<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;cstdlib&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;new&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;array&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> <span style=\"color: #0000ff;\">const<\/span> MY_SIZE= 10;\r\n\r\nstd::array&lt;<span style=\"color: #2b91af;\">void<\/span>* ,MY_SIZE&gt; myAlloc{nullptr,};\r\n    \r\n<span style=\"color: #2b91af;\">void<\/span>* <span style=\"color: #0000ff;\">operator<\/span> new(std::<span style=\"color: #2b91af;\">size_t<\/span> sz){\r\n    <span style=\"color: #0000ff;\">static<\/span> <span style=\"color: #2b91af;\">int<\/span> counter{};\r\n    <span style=\"color: #2b91af;\">void<\/span>* ptr= std::malloc(sz);\r\n    myAlloc.at(counter++)= ptr;\r\n    <span style=\"color: #0000ff;\">return<\/span> ptr;\r\n}\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> delete(<span style=\"color: #2b91af;\">void<\/span>* ptr) noexcept{\r\n    <span style=\"color: #0000ff;\">auto<\/span> ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(),myAlloc.end(),ptr));\r\n    myAlloc[ind]= nullptr;\r\n    std::free(ptr);\r\n}\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> getInfo(){\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n     \r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Not deallocated: \"<\/span> &lt;&lt; std::endl;\r\n    <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span> i: myAlloc){\r\n        <span style=\"color: #0000ff;\">if<\/span> (i != nullptr ) std::cout &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span> &lt;&lt; i &lt;&lt; std::endl;\r\n    }\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n    \r\n}\r\n\r\n<span style=\"color: #0000ff;\">#endif <\/span><span style=\"color: #008000;\">\/\/ MY_NEW2<\/span>\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The key idea is to use the static array <span style=\"font-family: courier new,courier;\">myAlloc<\/span> (line 15) to keep track of the addresses of all <span style=\"font-family: courier new,courier;\">std::malloc<\/span> (line 19) and <span style=\"font-family: courier new,courier;\">std::free<\/span> (line 19) invocations. Of course, I can not use the function <span style=\"font-family: courier new,courier;\">operator new<\/span> container that needs dynamic memory. This container would invoke the operator new. A recursion that would cause my program to crash. Therefore, I use a <span style=\"font-family: courier new,courier;\">std::array<\/span> in line 15 because<span style=\"font-family: courier new,courier;\"> std::array<\/span> gets its memory at compile time. Now it can happen that my <span style=\"font-family: courier new,courier;\">std::array<\/span> becomes too small. Therefore, I invoke <span style=\"font-family: courier new,courier;\">myAlloc.at(counter++) <\/span>to check the array boundaries<span style=\"font-family: courier new,courier;\">.<\/span><\/p>\n<p>Which memory address have I forgotten to release? The output answers.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5106\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew2.png\" alt=\"myNew2\" style=\"margin: 15px;\" width=\"364\" height=\"223\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew2.png 364w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew2-300x184.png 300w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><\/p>\n<p>A simple search for the object having the address is no good idea. Because it is quite probable that a new call of <span style=\"font-family: courier new,courier;\">std::malloc<\/span> reuses an already-used address. That is ok if the objects have been deleted in the meantime.<\/p>\n<p>But why are the addresses part of the solution? I have only to compare the memory address of the created objects with those of the not deleted objects.<\/p>\n<h2>Comparison of the memory addresses<\/h2>\n<p>In addition to the memory address, I have the reserved memory size at my disposal. I use this information in <span style=\"font-family: courier new,courier;\">operator new.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ myNew3.hpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#ifndef MY_NEW3<\/span>\r\n<span style=\"color: #0000ff;\">#define MY_NEW3<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;algorithm&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;cstdlib&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;new&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;array&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> <span style=\"color: #0000ff;\">const<\/span> MY_SIZE= 10;\r\n\r\nstd::array&lt;<span style=\"color: #2b91af;\">void<\/span>* ,MY_SIZE&gt; myAlloc{nullptr,};\r\n    \r\n<span style=\"color: #2b91af;\">void<\/span>* <span style=\"color: #0000ff;\">operator<\/span> new(std::<span style=\"color: #2b91af;\">size_t<\/span> sz){\r\n    <span style=\"color: #0000ff;\">static<\/span> <span style=\"color: #2b91af;\">int<\/span> counter{};\r\n    <span style=\"color: #2b91af;\">void<\/span>* ptr= std::malloc(sz);\r\n    myAlloc.at(counter++)= ptr;\r\n    std::cerr &lt;&lt; <span style=\"color: #a31515;\">\"Addr.: \"<\/span> &lt;&lt; ptr &lt;&lt; <span style=\"color: #a31515;\">\" size: \"<\/span> &lt;&lt; sz &lt;&lt; std::endl;\r\n    <span style=\"color: #0000ff;\">return<\/span> ptr;\r\n}\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> <span style=\"color: #0000ff;\">operator<\/span> delete(<span style=\"color: #2b91af;\">void<\/span>* ptr) noexcept{\r\n    <span style=\"color: #0000ff;\">auto<\/span> ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(),myAlloc.end(),ptr));\r\n    myAlloc[ind]= nullptr;\r\n    std::free(ptr);\r\n}\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> getInfo(){\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n     \r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Not deallocated: \"<\/span> &lt;&lt; std::endl;\r\n    <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span> i: myAlloc){\r\n        <span style=\"color: #0000ff;\">if<\/span> (i != nullptr ) std::cout &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span> &lt;&lt; i &lt;&lt; std::endl;\r\n    }\r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n    \r\n}\r\n\r\n<span style=\"color: #0000ff;\">#endif <\/span><span style=\"color: #008000;\">\/\/ MY_NEW3<\/span>\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Now, the allocation and deallocation of the application are more transparent.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5107\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew3.png\" alt=\"myNew3\" style=\"margin: 15px;\" width=\"364\" height=\"307\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew3.png 364w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/myNew3-300x253.png 300w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><\/p>\n<p>A simple comparison shows. I forget to release an object with 4 bytes and an object with 400 bytes. In addition, the allocation sequence in the source code corresponds to the sequence of outputs in the program. Now, it should be quite easy to identify the missing memory releases.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The program is not beautiful in two ways. First, I statically allocate the memory for std::array; second, I want to know which object was not released. In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/overloading-operator-new-and-delete-2\">next post, <\/a>I will solve both issues.&nbsp; &nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It happens quite too often that a C++ application allocates memory but doesn&#8217;t deallocate it. This is the job of the operator to new and delete. Thanks to them both, you can explicitly manage the memory management of an application.<\/p>\n","protected":false},"author":21,"featured_media":5105,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[498,493],"class_list":["post-5108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-memory","tag-new-delete"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5108"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5108\/revisions"}],"predecessor-version":[{"id":6905,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5108\/revisions\/6905"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5105"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}