96
96
97
97
#include < math.h>
98
98
#include < complex.h>
99
+ #include < memory> // unique_ptr
99
100
100
101
namespace xsf {
101
102
namespace amos {
@@ -2362,6 +2363,7 @@ inline int besy(
2362
2363
// CANCE BY ARGUMENT REDUCTION
2363
2364
// IERR=5, ERROR - NO COMPUTATION,
2364
2365
// ALGORITHM TERMINATION CONDITION NOT MET
2366
+ // IERR=6, Memory allocation failed.
2365
2367
//
2366
2368
// ***LONG DESCRIPTION
2367
2369
//
@@ -2450,7 +2452,6 @@ inline int besy(
2450
2452
std::complex<double > c1, c2, hci, st;
2451
2453
double elim, exr, exi, ey, tay, xx, yy, ascle, rtol, atol, tol, aa, bb, r1m5;
2452
2454
int i, k, k1, k2, nz, nz1, nz2;
2453
- std::complex<double >* cwrk = new std::complex<double >[n];
2454
2455
2455
2456
xx = std::real (z);
2456
2457
yy = std::imag (z);
@@ -2467,7 +2468,14 @@ inline int besy(
2467
2468
nz1 = besh (z, fnu, kode, 1 , n, cy, ierr);
2468
2469
if ((*ierr != 0 ) && (*ierr != 3 )) { return 0 ; }
2469
2470
2470
- nz2 = besh (z, fnu, kode, 2 , n, cwrk, ierr);
2471
+ auto cwrk = std::unique_ptr<std::complex<double >[]>
2472
+ {new (std::nothrow) std::complex<double >[n]};
2473
+ if (cwrk == nullptr ) {
2474
+ *ierr = 6 ; // Memory allocation failed.
2475
+ return 0 ;
2476
+ }
2477
+
2478
+ nz2 = besh (z, fnu, kode, 2 , n, cwrk.get (), ierr);
2471
2479
if ((*ierr != 0 ) && (*ierr != 3 )) { return 0 ; }
2472
2480
2473
2481
nz = (nz1 > nz2 ? nz2 : nz1);
@@ -2531,7 +2539,6 @@ inline int besy(
2531
2539
if ((st == 0.0 ) && (ey == 0.0 )) { nz += 1 ; }
2532
2540
}
2533
2541
2534
- delete [] cwrk;
2535
2542
return nz;
2536
2543
}
2537
2544
0 commit comments