Stack Allocation at Run-Time
In ATS, there is support for allocating memory at run-time in the stack
frame of the calling function, and it is guaranteed by the type system of ATS
that the memory thus allocated cannot be accessed once the calling function
returns.
In the following contrived example, the implemented function
name_of_month_1 allocates in its stack frame an array of size 12
that is initialized with the names of 12 months, and then returns the name
of the ith month, where i is an integer between 1 and 12:
The following syntax means that the starting address of the allocated
array is stored in
p_arr while the view of the array is stored in
pf_arr:
This allocated array is initialized with the strings representing the names
of the 12 months: "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec".
A variant of the function name_of_month_1 is implemeneted as follows:
The following syntax means that the function
name_of_month_2
allocates a string array of size 12 in its stack frame and initializes the
array with the empty string:
The starting address and the view of the allocated array are stored in
p_arr and
pf_arr, respectively. If the following syntax
is used:
then the allocated array is uninitialized, that is, the view of the proof
pf_arr is
[string?][12] @ p_arr (instead of
[string][12] @ p_arr).
When higher-order functions are employed in systems programming, it is
often desirable to form closures in the stack frame of the calling function
so as to avoid the need for memory allocation on heap. In the following
example, the implemented function print_month_name forms a closure
in its stack frame, which is then passed to a higher-order function
iforeach_array_ptr_clo:
Note that the keyword
@lam (instead of
lam) is used here
to indicate that the closure is constructed in the stack frame of the
function
print_month_names.