No need to pre-check, just try to allocate memory, and if you can’t, then catch the exception. In this case, it has a type bad_alloc.
#include <iostream>
#include <new> // included for std::bad_alloc
double* allocateMemory(int memorySize)
{
double* tReturn = NULL;
try
{
tReturn = new double[memorySize];
}
catch (bad_alloc& badAlloc)
{
cerr << "bad_alloc caught, not enough memory: " << badAlloc.what() << endl;
}
return tReturn;
};
. - deallocateMemory , NULL, delete - .
void deallocateMemory(double* &dMemorySize)
{
delete[] dMemorySize;
dMemorySize = NULL;
};
, :
double *chunkNew = heap.allocateMemory(hMemory);
heap.deallocateMemory(chunkNew);
heap.deallocateMemory(chunkNew);