# Memory release of array initialization

**URL:** <https://discourse.ats-lang.org/t/memory-release-of-array-initialization/217>\
**Category:** Google Group\
**Created:** [June 13, 2013, 2:00pm UTC](https://discourse.ats-lang.org/t/memory-release-of-array-initialization/217 "2013-06-13T14:00:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [June 13, 2013, 2:00pm UTC](https://discourse.ats-lang.org/t/memory-release-of-array-initialization/217/1 "2013-06-13T14:00:11Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [June 13, 2013, 2:38pm UTC](https://discourse.ats-lang.org/t/memory-release-of-array-initialization/217/2 "2013-06-13T14:38:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [June 13, 2013, 2:35pm UTC](https://discourse.ats-lang.org/t/memory-release-of-array-initialization/217/3 "2013-06-13T14:35:53Z")

</div>

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).
