title: How can I check if there is enough heap memory available?
body: <p>I have an assignment that requires me to create a "Heap" class that allocates and deallocates memory.  I believe that my code works and the solution builds and runs properly but I want to make sure that I am not getting any memory leaks.  I also need to add some code that checks if the desired amount to be allocated to the heap is even available...if someone were to allocate a very large amount.  How is it possible to check if the memory allocated on the heap is available or NULL if there is not enough memory.  Here is my code so far:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class Heap{
  public:
  
  double* allocateMemory(int memorySize)
  {
      return new double[memorySize];
  };
  void deallocateMemory(double* dMemorySize)
  {
      delete[] dMemorySize;
  };
  
  };
  
  int main()
  {
  Heap heap;
  cout &lt;&lt; "Enter the number of double elements that you want to allocate: " &lt;&lt; endl;
  int hMemory;
  const int doubleByteSize = 8;
  cin &gt;&gt; hMemory;
  
  double *chunkNew = heap.allocateMemory(hMemory);
  
  cout &lt;&lt; "The amount of space you took up on the heap is: " &lt;&lt;
           hMemory*doubleByteSize &lt;&lt; " bytes" &lt;&lt; 
       starting at address: " &lt;&lt; "\n" &lt;&lt; &amp;hMemory &lt;&lt; endl; 
  
  heap.deallocateMemory(chunkNew);
  
  system("pause");
  return 0;
  }
  </code></pre>