Skip to content

Unsafe

Zap keeps raw pointer work behind explicit unsafe.

Unsafe features must be enabled at compile time:

Terminal window
./zap/build/zapc --allow-unsafe program.zp
fun main() Int {
var value: Int = 41;
unsafe {
var ptr: *Int = &value;
*ptr = *ptr + 1;
}
return value;
}
unsafe fun deref(ptr: *Int) Int {
return *ptr;
}
struct Box {
ptr: *Int
}
  • address-of with &value
  • raw pointer types like *Int and *Void
  • dereference with *ptr
  • pointer arithmetic such as ptr + i
  • casts with as
  • manual allocation through std/mem
import "std/mem";
fun main() Int {
unsafe {
var ptr: *Int = mem.malloc(3 * 8) as *Int;
if ptr == null {
return 1;
}
*ptr = 10;
*(ptr + 1) = 20;
*(ptr + 2) = 30;
mem.free(ptr as *Void);
return 0;
}
}

Use unsafe sparingly. The point is explicit escape hatches, not making the whole language unchecked by default.