memory->destroy function for 2d array

Dear all,

I have a simple question about LAMMPS’s method to free 2d array. The function template for 2d array is as the following:

template

void destroy(TYPE **array)
{

if (array == NULL) return;

sfree(array[0]);

sfree(array);
}

The sfree function is similar to tradition C-style free function. I’m curious if the memory is fully freed since only memory corresponding to pointer array[0] is freed. How about array[1], array[2] and etc.? I remember for a 2d array double **ptr, we should free it by code like the following:

for(int i = 0; i < N; i++)
free(ptr[i]);
free(ptr);

Thank you!

Dear all,

I have a simple question about LAMMPS's method to free 2d array. The
function template for 2d array is as the following:

template <typename TYPE>

void destroy(TYPE **array)
{

   if (array == NULL) return;

   sfree(array[0]);

   sfree(array);
}

The sfree function is similar to tradition C-style free function. I'm
curious if the memory is fully freed since only memory corresponding to

yes, it is fully freed. you can easily verify this from running with
valgrind's memory checker. there are no memory leaks due to this.
if i run the bench/in.lj input via valgrind using a serial executable,
i get this summary:

==16222==
==16222== HEAP SUMMARY:
==16222== in use at exit: 0 bytes in 0 blocks
==16222== total heap usage: 1,608 allocs, 1,608 frees, 25,999,335
bytes allocated
==16222==
==16222== All heap blocks were freed -- no leaks are possible
==16222==
==16222== For counts of detected and suppressed errors, rerun with: -v
==16222== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

the 2d arrays are used for all per-atom data. so there would
definitely be a memory leak report here, if the destroy method would
not work properly.

pointer array[0] is freed. How about array[1], array[2] and etc.? I remember
for a 2d array double **ptr, we should free it by code like the following:

that depends on how the memory is allocated. what you describe is
needed, if each individual array may be of different size. in the case
of LAMMPS however, the whole 2d array storage is allocated in just one
contiguous block. array[1], array[2] and so on are just initialized
with the corresponding offsets. check out the source for the
corresponding "create" template in memory.h

axel.