Skip to content

Pointer and Reference

squid233 edited this page Jan 27, 2025 · 7 revisions

The pointer type of method parameters are all converted to MemorySegment.

Access and Assign

No-const pointer types can be assigned from native functions.

// C: void load(int* results)
void load(MemorySegment results);
void load(SegmentAllocator allocator, int[] results) {
    MemorySegment seg = MemoryUtil.allocArray(allocator, results);
    load(seg);
    MemoryUtil.copy(seg, results);
}

For const array type, simply allocate an array.

// C: void set(int* const values);
void set(MemorySegment values);
void set(SegmentAllocator allocator, int[] values) {
    set(MemoryUtil.allocArray(allocator, values));
}
Clone this wiki locally