{"id":5110,"date":"2017-01-01T06:47:28","date_gmt":"2017-01-01T06:47:28","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/overloading-operator-new-and-delete-2\/"},"modified":"2023-06-26T12:29:04","modified_gmt":"2023-06-26T12:29:04","slug":"overloading-operator-new-and-delete-2","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/overloading-operator-new-and-delete-2\/","title":{"rendered":"Overloading Operator new and delete 2"},"content":{"rendered":"<p>I overloaded in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/overloading-operator-new-and-delete\">last post <\/a><span style=\"font-family: 'Courier New', Courier, monospace;\">operator new <\/span>and <span style=\"font-family: 'Courier New', Courier, monospace;\">delete<\/span>. Therefore, finding memory leaks and getting the first hint of the bad guys. My solution had two not-so-nice properties. With this post, I will overcome them.<\/p>\n<p><!--more--><\/p>\n<p>What were the not-so-nice properties of my last post? At first, I got only a hint that my memory was lost; second, I had to prepare the whole bookkeeping of memory management at compile time. Those who want to know the details of these shortcomings should read the last post. But now the ugliness will go.<\/p>\n<h2>Who is the bad guy?<\/h2>\n<p>Particular tasks ask for special forces. So I have to use a small macro for debugging purposes.<\/p>\n<p>I want to stress this point. <b>I&#8217;m not a friend of macros.<\/b><\/p>\n<p>Let&#8217;s have a look at the macro. <span style=\"font-family: courier        new,courier;\">#define new new(__FILE__, __LINE__)<\/span><\/p>\n<p>The macro causes each <span style=\"font-family: 'Courier New', Courier, monospace;\">new<\/span> call will be mapped to the overloaded <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>call. This overloaded <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>call adds the name of the file and the line number, respectively. That&#8217;s exactly the information I need.<\/p>\n<p>But what will happen if I use the macro in line 6?<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ overloadOperatorNewAndDelete2.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include \"myNew4.hpp\"<\/span>\r\n<span style=\"color: #008000;\">\/\/ #include \"myNew5.hpp\"<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#define new new(__FILE__, __LINE__)<\/span>\r\n\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\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<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    dummyFunction();\r\n    \r\n    getInfo();\r\n    \r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The preprocessor substitutes all <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>calls. That shows precisely the modified <span style=\"font-family: 'Courier New', Courier, monospace;\">main <\/span>function.<\/p>\n<p>&nbsp;<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><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: #a31515;\">\"overloadNewAndDelete.cpp\"<\/span>, 14) <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<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: #a31515;\">\"overloadNewAndDelete.cpp\"<\/span>, 24) <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: #a31515;\">\"overloadNewAndDelete.cpp\"<\/span>, 25) <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: #a31515;\">\"overloadNewAndDelete.cpp\"<\/span>, 26) <span style=\"color: #2b91af;\">double<\/span>[2]{1.1,1.2};\r\n\r\n    MyClass* myClass= <span style=\"color: #0000ff;\">new<\/span>(<span style=\"color: #a31515;\">\"overloadNewAndDelete.cpp\"<\/span>, 28) MyClass;\r\n    MyClass2* myClass2= <span style=\"color: #0000ff;\">new<\/span>(<span style=\"color: #a31515;\">\"overloadNewAndDelete.cpp\"<\/span>, 29) 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    dummyFunction();\r\n\r\n    getInfo();\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Lines 2 and 12 show that the preprocessor substitutes the constants <span style=\"font-family: courier new,courier;\">__FILE__<\/span> and <span style=\"font-family: courier new,courier;\">__LINE__<\/span> in the macro. But how does the magic work? The header <span style=\"font-family: 'Times New Roman', Times, serif;\"><span style=\"font-family: 'Courier New', Courier, monospace;\">myNew4<\/span>.<\/span><span style=\"font-family: 'Courier New', Courier, monospace;\">hpp <\/span>solves the riddle. <span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p>&nbsp;<\/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\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\r\n57\r\n58<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ myNew4.hpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#ifndef MY_NEW4<\/span>\r\n<span style=\"color: #0000ff;\">#define MY_NEW4<\/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;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\n<span style=\"color: #2b91af;\">int<\/span> counter= 0;\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>* newImpl(std::<span style=\"color: #2b91af;\">size_t<\/span> sz,<span style=\"color: #2b91af;\">char<\/span> <span style=\"color: #0000ff;\">const<\/span>* file, <span style=\"color: #2b91af;\">int<\/span> line){\r\n    <span style=\"color: #2b91af;\">void<\/span>* ptr= std::malloc(sz);\r\n    std::cerr &lt;&lt; file &lt;&lt; <span style=\"color: #a31515;\">\": \"<\/span> &lt;&lt; line &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span> &lt;&lt;  ptr &lt;&lt; std::endl;\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> new(std::<span style=\"color: #2b91af;\">size_t<\/span> sz,<span style=\"color: #2b91af;\">char<\/span> <span style=\"color: #0000ff;\">const<\/span>* file, <span style=\"color: #2b91af;\">int<\/span> line){  \r\n    <span style=\"color: #0000ff;\">return<\/span> newImpl(sz,file,line);\r\n}\r\n<span id=\"transmark\"><\/span>\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> sz,<span style=\"color: #2b91af;\">char<\/span> <span style=\"color: #0000ff;\">const<\/span>* file, <span style=\"color: #2b91af;\">int<\/span> line){  \r\n    <span style=\"color: #0000ff;\">return<\/span> newImpl(sz,file,line);\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    <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: #0000ff;\">#define new new(__FILE__, __LINE__)<\/span>\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> dummyFunction(){\r\n    <span style=\"color: #2b91af;\">int<\/span>* dummy= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>;\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;\">\"Allocation: \"<\/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_NEW4<\/span>\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I implement in line 25 and 29 the special operators <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>and <span style=\"font-family: 'Courier New', Courier, monospace;\">new[] <\/span>that delegates their functionality to the helper function <span style=\"font-family: 'Courier New', Courier, monospace;\">newImpl <\/span>(lines 18 &#8211; 23). The function does two important jobs. At first, it displays to each <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>call the name of the source file and the line number (line 20); second, it keeps in the static array <span style=\"font-family: 'Courier New', Courier, monospace;\">myAlloc <\/span>track of each used memory address (line 21). This fits the behavior of the overloaded operator <span style=\"font-family: 'Courier New', Courier, monospace;\">delete <\/span>that sets all memory addresses to the null pointer <span style=\"font-family: 'Courier New', Courier, monospace;\">nullptr<\/span> (line 35). The memory addresses stand for the deallocated memory areas. In the end, the function <span style=\"font-family: 'Courier New', Courier, monospace;\">getInfo <\/span>displays the memory addresses that were not deallocated. You can directly see them together with the file name and line number.<\/p>\n<\/p>\n<p>Of course, I can directly apply the macro in the file <span style=\"font-family: courier new,courier;\">myNew4.hpp<\/span>. That was a lot of theory. What&#8217;s the output of the program?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5109\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/overloadNewAndDelete.png\" alt=\"overloadNewAndDelete\" width=\"481\" height=\"334\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/overloadNewAndDelete.png 481w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/overloadNewAndDelete-300x208.png 300w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/>&nbsp;<\/p>\n<p>The memory areas to the memory address 0x8c3010, 0x8c3090, and 0x8c3230 were not deallocated. The bad guys are the <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>calls in line 24 and line 14 <span style=\"font-family: courier new,courier;\"><\/span>(<span style=\"font-family: courier new,courier;\">overloadNewAndDelete.hpp<\/span>) and the <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>call in line 42 <span style=\"font-family: courier new,courier;\"><\/span>(<span style=\"font-family: courier new,courier;\">myNew4.hpp<\/span>).<\/p>\n<p>Impressed? I assume, yes. But the presented technique has two drawbacks. One minor and one major.<\/p>\n<ol>\n<li>I have to overload the simple operator <span style=\"font-family: courier new,courier;\">new<\/span> and the operator<span style=\"font-family: courier new,courier;\"> new []<\/span> for arrays. This, because of overloaded operator new,&nbsp; is not a fallback for the <a href=\"https:\/\/www.modernescpp.com\/index.php\/overloading-operator-new-and-delete\">three remaining operators <span style=\"font-family: 'Courier New', Courier, monospace;\">new. <\/span><\/a><\/li>\n<li>I can not use the particular operator <span style=\"font-family: courier          new,courier;\">new<\/span> that returns in the error case a null pointer. Because it will be explicitly called by the operator <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>with the argument <span style=\"font-family: courier new,courier;\">std::nothrow<\/span>: <span style=\"font-family: courier new,courier;\">int* myInt= new (std::nothrow) int(1998);<\/span><\/li>\n<\/ol>\n<p>Now, I have to solve the first issue. I want to use for the array <span style=\"font-family: 'Courier New', Courier, monospace;\">myAlloc, <\/span>a data structure that manages its memory at run time. Therefore, it is not necessary anymore to eagerly allocate the memory at compile time.<\/p>\n<h2>All at run time<\/h2>\n<p>What was the reason that I couldn&#8217;t allocate memory in the <span style=\"font-family: 'Courier New', Courier, monospace;\">operator new<\/span>? The <span style=\"font-family: 'Courier New', Courier, monospace;\">operator new <\/span>was globally overloaded. Therefore, a call of <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>would end in a never-ending recursion. That will happen exactly if I use a container like <span style=\"font-family: 'Courier New', Courier, monospace;\">std::vector <\/span>that dynamically allocates its memory.<\/p>\n<p>This restriction holds not anymore because I didn&#8217;t overload the global <span style=\"font-family: 'Courier New', Courier, monospace;\">operator new. <\/span>That is a fallback to the three remaining <span style=\"font-family: 'Courier New', Courier, monospace;\">new <\/span>operators. Thanks to the macro, my variant of <span style=\"font-family: 'Courier New', Courier, monospace;\">operator new <\/span>is used. Therefore, I can use <span style=\"font-family: 'courier new';\">std::<\/span><span style=\"font-family: 'Courier New', Courier, monospace;\">vector <\/span>in my <span style=\"font-family: 'Courier New', Courier, monospace;\">operator new. <\/span><\/p>\n<p>Exactly that can you see in my program.<\/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\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ myNew5.hpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#ifndef MY_NEW5<\/span>\r\n<span style=\"color: #0000ff;\">#define MY_NEW5<\/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;vector&gt;<\/span>\r\n\r\nstd::vector&lt;<span style=\"color: #2b91af;\">void<\/span>*&gt; myAlloc;\r\n\r\n<span style=\"color: #2b91af;\">void<\/span>* newImpl(std::<span style=\"color: #2b91af;\">size_t<\/span> sz,<span style=\"color: #2b91af;\">char<\/span> <span style=\"color: #0000ff;\">const<\/span>* file, <span style=\"color: #2b91af;\">int<\/span> line){\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    std::cerr &lt;&lt; file &lt;&lt; <span style=\"color: #a31515;\">\": \"<\/span> &lt;&lt; line &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span> &lt;&lt;  ptr &lt;&lt; std::endl;\r\n    myAlloc.push_back(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> new(std::<span style=\"color: #2b91af;\">size_t<\/span> sz,<span style=\"color: #2b91af;\">char<\/span> <span style=\"color: #0000ff;\">const<\/span>* file, <span style=\"color: #2b91af;\">int<\/span> line){  \r\n    <span style=\"color: #0000ff;\">return<\/span> newImpl(sz,file,line);\r\n}\r\n\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> sz,<span style=\"color: #2b91af;\">char<\/span> <span style=\"color: #0000ff;\">const<\/span>* file, <span style=\"color: #2b91af;\">int<\/span> line){  \r\n    <span style=\"color: #0000ff;\">return<\/span> newImpl(sz,file,line);\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    <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: #0000ff;\">#define new new(__FILE__, __LINE__)<\/span>\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> dummyFunction(){\r\n    <span style=\"color: #2b91af;\">int<\/span>* dummy= <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>;\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;\">\"Allocation: \"<\/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_NEW5<\/span>\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I use in lines 13, 19, and 33 <span style=\"font-family: 'courier new';\">std<\/span><span style=\"font-family: 'Courier New', Courier, monospace;\">::vector. <\/span><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/memory-management-with-std-allocator\">next post, <\/a>I will have a closer look at&nbsp;<span style=\"font-family: 'courier new';\">std<\/span>::<span style=\"font-family: 'courier new';\">allocator<\/span>. In particular, I&#8217;m interested how memory requests can be mapped to particular memory areas.<\/p>\n<p><span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I overloaded in the last post operator new and delete. Therefore, finding memory leaks and getting the first hint of the bad guys. My solution had two not-so-nice properties. With this post, I will overcome them.<\/p>\n","protected":false},"author":21,"featured_media":5109,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[498,493],"class_list":["post-5110","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\/5110","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=5110"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5110\/revisions"}],"predecessor-version":[{"id":6904,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5110\/revisions\/6904"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5109"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}