title: Allocate maximum memory available
body: <p>I currently try to write a programm which is supposed to allocate the maximum memory available. I came to a solution which limits the area of potential available memory until both borders are equal (see listing)</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void enforceMemoryLeakage(void** arrayOfAllocMemory)
  {
      unsigned int maxMemory = 0x80000000;
      unsigned int minMemory = 0x50000000;
      unsigned int attempAllocatedMemory = minMemory + (maxMemory - minMemory) / 2;
      void* pAllocMemory;
  
      while((maxMemory - minMemory) &gt; 1)
      {
          pAllocMemory = malloc(attempAllocatedMemory);
  
          if (pAllocMemory != NULL)
          {
              minMemory = attempAllocatedMemory;
              attempAllocatedMemory += (maxMemory - minMemory) / 2;
              free(pAllocMemory);
          }
          else
          {
              maxMemory = attempAllocatedMemory;
              attempAllocatedMemory = minMemory + (maxMemory - minMemory) / 2;
          }
      }
      arrayOfAllocMemory[0] = malloc(maxMemory);
      void* pAllocAdditionalMemory = malloc(100);
  
      if (pAllocAdditionalMemory == NULL)
          std::cout &lt;&lt; "Maximum memory: " &lt;&lt; minMemory &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>The above displayed code works fine. However if the command is executed</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void* pAllocAdditionalMemory = malloc(100);
  
  if (pAllocAdditionalMemory == NULL)
      std::cout &lt;&lt; "Maximum memory: " &lt;&lt; minMemory &lt;&lt; "\n";
  </code></pre>
  
  <p>I would have expected that there is no further memory available. However it does not work which brings me to my actual question why the above shown approach does not work.</p>
  
  <p>Best regards</p>
  
  <p>Ratbald</p>