To get acquainted with new operator which does not generate exceptions see the article "Nothrow new". And here I will give only two examples which demonstrate the idea of the solution very clear.
The code with processing of an exception:
size_t errCount = 0;
#pragma omp parallel for num_threads(4) reduction(+: errCount)
for(int i = 0; i < 4; i++)
{
try {
float *ptr = new float[10000];
// code
delete [] ptr;
}
catch (std::bad_alloc &)
{
++errCount;
}
}Simplified code without processing of exceptions:size_t errCount = 0;
#pragma omp parallel for num_threads(4) reduction(+: errCount)
for(int i = 0; i < 4; i++)
{
float *ptr = new(std::nothrow) float[10000];
if (ptr == NULL) {
++errCount;
} else {
// code
delete [] ptr;
}
}
Комментариев нет:
Отправить комментарий