title: Recursive calls when trying to wrap and override the global operator new
body: <p>I have not programmed C++ for a while and encountered a strange behavior when toying with overloaded global operators <code>new</code> and <code>delete</code>.
  The essence of the problem seems to be that a wrapper build around the default global <code>new</code> and residing in a <i>separate</i> source file 
  nevertheless calls an <code>operator new</code>  overloaded in another (and separately compiled so) source file.</p>
  
  <p>Why is it so, i.e. which language rules / features am I violating / misusing?</p>
  
  <p>Thanks in advance, the details are below.</p>
  
  <p>Project structure:</p>
  
  <pre><code>.
  ..
  main.cpp
  mem_wrappers.h
  mem_wrappers.cpp
  </code></pre>
  
  <p>Project files contents:</p>
  
  <p><b>main.cpp</b></p>
  
  <pre><code>#include "./mem_wrappers.h"
  #include &lt;iostream&gt;
  #include &lt;cstring&gt;
  
  
  void* operator new[] (size_t sz) throw (std::bad_alloc) {
    std::cout &lt;&lt; "overloaded new()[]" &lt;&lt; std::endl;
      return default_arr_new_wrapper(sz);
  }
  
  
  int main() {
      const unsigned num = 5;
      int * i_arr = new int [num];
  
      return 0;
  }
  </code></pre>
  
  <p><b>mem_wrappers.h</b></p>
  
  <pre><code>#include &lt;cstring&gt;
  
  
  void * default_arr_new_wrapper(size_t sz);
  </code></pre>
  
  <p><b>mem_wrappers.cpp</b></p>
  
  <pre><code>#include &lt;new&gt;
  #include &lt;cstring&gt;
  #include &lt;iostream&gt;
  
  
  void * default_arr_new_wrapper(size_t sz) {
      std::cout &lt;&lt; "default_arr_new wrapper()" &lt;&lt; std::endl;
      return ::operator new[](sz);
  }
  </code></pre>
  
  <p>Complied with <i>g++ main.cpp mem_wrappers.cpp --ansi --pedantic -Wall</i> when run gives endless <code>operator new[]</code> to <code>default_arr_new_wrapper</code> and vice versa calls resulting in the following output:</p>
  
  <pre><code>overloaded new()[]
  
  default_arr_new_wrapper()
  
  overloaded new()[]
  
  default_arr_new_wrapper()
  
  ...
  </code></pre>
  
  <p>and, finally, in SO (the MS Visual Studio Express compiler behaves alike).</p>
  
  <p>Compilers I use: <b>gcc version 3.4.2 (mingw-special)</b> and <b>MS Visual Studio 2008 Version 9.0.30729.1 SP</b>.</p>
  
  <p>EDIT / UPDATE</p>
  
  <p>If (as the first answers and comments suggest) the global <code>operator new</code> is effectively redefined for the whole executable just by overloading it [the operator] in a single compilation unit, then:</p>
  
  <p>is it that merely linking with an object file whose source overloads the global <code>operator new</code> makes any application or library change its (well, lets  call it so) memory allocation policy? Is thus this overload operator efficiently providing a hook for the language runtime (I mean what's with those linker symbols in the already-compiled-without-any-overloaded-new object files, is it that there can be only one <code>new</code>)?</p>
  
  <p>P.S. I know that <code>malloc</code>  and <code>free</code> would do, and already tried them before posting here (worked ok), but nevertheless, what's behind this behavior (and what if I was actually to wrap the default <code>operator new</code> anyway? :)) ?</p>