Skip to content

Commit 742ee12

Browse files
author
wenfei-li
committed
gint : fix alloc-dealloc-mismatch
1 parent 7caf5ce commit 742ee12

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

source/module_gint/gint_tools.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace Gint_Tools
3939
const int jby,
4040
const int kbz)
4141
{
42-
int *vindex = (int*)malloc(GlobalC::pw.bxyz*sizeof(int));
42+
int *vindex = new int[GlobalC::pw.bxyz*sizeof(int)];
4343
int bindex=0;
4444
// z is the fastest,
4545
// ipart can be obtained by using a previously stored array
@@ -71,12 +71,12 @@ namespace Gint_Tools
7171
{
7272
// set the index for obtaining local potentials
7373
int* vindex = Gint_Tools::get_vindex(ncyz, ibx, jby, kbz);
74-
double *vldr3 = (double*)malloc(GlobalC::pw.bxyz*sizeof(double));
74+
double *vldr3 = new double[GlobalC::pw.bxyz*sizeof(double)];
7575
for(int ib=0; ib<GlobalC::pw.bxyz; ib++)
7676
{
7777
vldr3[ib]=vlocal[vindex[ib]] * dv;
7878
}
79-
free(vindex); vindex=nullptr;
79+
delete[] vindex;
8080
return vldr3;
8181
}
8282

@@ -88,12 +88,12 @@ namespace Gint_Tools
8888
{
8989
// set the index for obtaining local potentials
9090
int* vindex = Gint_Tools::get_vindex(start_ind, ncyz);
91-
double *vldr3 = (double*)malloc(GlobalC::pw.bxyz*sizeof(double));
91+
double *vldr3 = new double[GlobalC::pw.bxyz*sizeof(double)];
9292
for(int ib=0; ib<GlobalC::pw.bxyz; ib++)
9393
{
9494
vldr3[ib]=vlocal[vindex[ib]] * dv;
9595
}
96-
free(vindex); vindex=nullptr;
96+
delete[] vindex;
9797
return vldr3;
9898
}
9999

source/module_gint/gint_tools.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ namespace Gint_Tools
243243
template<typename T>
244244
Array_Pool<T>::Array_Pool(const int nr, const int nc) // Attention: uninitialized
245245
{
246-
ptr_1D = (T*)malloc(nr*nc*sizeof(T));
247-
ptr_2D = (T**)malloc(nr*sizeof(T*));
246+
ptr_1D = new T[nr*nc*sizeof(T)];
247+
ptr_2D = new T*[nr*sizeof(T*)];
248248
for (int ir=0; ir<nr; ++ir)
249249
ptr_2D[ir] = &ptr_1D[ir*nc];
250250
}
@@ -254,15 +254,15 @@ namespace Gint_Tools
254254
{
255255
ptr_1D = array.ptr_1D;
256256
ptr_2D = array.ptr_2D;
257-
free(array.ptr_2D); array.ptr_2D=nullptr;
258-
free(array.ptr_1D); array.ptr_1D=nullptr;
257+
delete[] array.ptr_2D;
258+
delete[] array.ptr_1D;
259259
}
260260

261261
template<typename T>
262262
Array_Pool<T>::~Array_Pool()
263263
{
264-
free(ptr_2D);
265-
free(ptr_1D);
264+
delete[] ptr_2D;
265+
delete[] ptr_1D;
266266
}
267267
}
268268

0 commit comments

Comments
 (0)