Memory release of array initialization

hello,
I have found two methods to get an initilized array:

implement main(argc, argv) = let
fun{a:t@ype} data{n: nat} (arr: &(@[a][n])): void = ()

var !p_arr1 = @[float](0.9f, ~1.8f, 2.7f, 3.6f, 4.5f, 5.4f, 6.3f, 7.2f,
8.1f, 9.0f)
val () = data(!p_arr1)

val (pf_gc, pf_arr2|p_arr2, sz) = $arrsz {float}(0.9f, 1.8f, 2.7f, 3.6f,
4.5f, 5.4f, 6.3f, 7.2f, 8.1f, 9.0f)
val () = data(!p_arr2)
in
array_ptr_free(pf_gc, pf_arr2|p_arr2)
end

memory of p_arr2 can be released with array_ptr_free

Is the memory allocated by p_arr1 is on the heap same as C and is released
when the scope of the function is destroyed or is it garbage collected ?

thanks :wink:

Sorry, I didn’t read your question carefully.

What I said above applies to array2. As for array1, it is allocated on the
stack
of the calling function; so it is freed once the calling function returns.

This all depends on how malloc/free is implemented.

If you don’t use GC, then the memory allocated for array is freed (by
calling free in libc).

If you use GC, then the memory is returned to GC.

Of course, one can implement array_ptr_free as a no-op function. If so, the
memory is
waiting to be garbage-collected (or leaked if there is no GC).