A statically allocated array

16 views
Skip to first unread message

Nick

unread,
Mar 16, 2026, 12:05:30 AM (5 days ago) Mar 16
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

Hongwei Xi

unread,
Mar 16, 2026, 6:18:01 PM (5 days ago) Mar 16
to ats-lan...@googlegroups.com
You may have to construct such a statically allocated array in C and then export it.
Something like this:

extern
val st: arrayref(point, 3) // statically allocated in C


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($UN.cast2ptr(st), 3)

  in
    println! ("The End.");
  end

Nick kost...@gmail.com
Unsubscribe

12:05 AM (18 hours ago)


to ats-lang-users
>> it is possible to do without temporary variable v

In principle, this should be ok as `st[1]` is a left-value. It is probably not supported in ATS2, though.


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/ats-lang-users/ecde188f-dbae-471f-aa5e-b1cecfe62c19n%40googlegroups.com.

Nick

unread,
Mar 17, 2026, 12:59:24 AM (4 days ago) Mar 17
to ats-lang-users
Thank you.
And I tried to do it in ATS, not in C :-)

I have another question.
Now I have the following code:


#define N 3
%{^
#define N 3
static Point st[N];
%}

extern val st: arrayref(point, N) = "mac#" // statically allocated in C

Is it possible for N to be common to C and ATS?

вторник, 17 марта 2026 г. в 00:18:01 UTC+2, gmhwxi:

Hongwei Xi

unread,
Mar 17, 2026, 1:32:42 AM (4 days ago) Mar 17
to ats-lan...@googlegroups.com
>> Is it possible for N to be common to C and ATS?

I remember I once created a header file that can be used in both ATS and C
to address this issue.

Reply all
Reply to author
Forward
0 new messages