Nick
unread,Mar 16, 2026, 12:05:30 AM (6 days ago) Mar 16Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ats-lang-users
Hello.
Please tell me how to do statically allocated array.
I'm looking at an example "Constructing a Statically Allocated List" in "Introduction to Programming in ATS"
but the magic of unsafe eludes me...
In the example below, I want to do "st" array statically...
#include "share/atspre_staload.hats"
staload UN = "prelude/SATS/unsafe.sats"
%{^
typedef struct Point {
int x;
int y;
} Point;
void print_point (Point* p) {
printf("C: %dx%d\n", p->x, p->y);
}
void process_points_in_c(Point* arr, int n) {
for (int i = 0; i < n; i++) {
printf("C sees Point[%d]: %d, %d\n", i, arr[i].x, arr[i].y);
}
}
%}
typedef point = $extype_struct "Point" of {
x = int,
y = int
}
extern fun print_point (p: &point): void = "mac#"
extern fun process_points_in_c (p: ptr, n: int): void = "mac#"
fun say_point (p: point): void = println! ("ATS: ", p.x, "x", p.y)
implement main0 (argc, argv) =
let
var st = @[point][3](@{x=0, y=0})
var p = @{x=1, y=1}: point
val () = st[1] := p
val () = say_point (st[1])
var v = st[1]
val () = print_point (v)
val () = st[2] := (@{x=2, y=2}: point)
val () = say_point (st[2])
val () = process_points_in_c(addr@st, 3)
in
println! ("The End.");
end
There is also a question about
var v = st[1]
val () = print_point (v)
It is possible to do without temporary variable v щоб передати ptr to C function.
Thanks