Pointers
Zig has two kinds of pointers:
*T
- pointer to exactly one item.- Supports deref syntax:
ptr.*
- Supports deref syntax:
[*]T
- pointer to unknown number of items.- Supports index syntax:
ptr[i]
- Supports slice syntax:
ptr[start..end]
- Supports pointer arithmetic:
ptr + x
,ptr - x
T
must have a known size, which means that it cannot bec_void
or any other opaque type.
- Supports index syntax:
These types are closely related to Arrays and Slices:
*[N]T
- pointer to N items, same as single-item pointer to array.- Supports index syntax:
array_ptr[i]
- Supports slice syntax:
array_ptr[start..end]
- Supports len property:
array_ptr.len
- Supports index syntax:
[]T
- pointer to runtime-known number of items.- Supports index syntax:
slice[i]
- Supports slice syntax:
slice[start..end]
- Supports len property:
slice.len
- Supports index syntax:
Use &x
to obtain a single-item pointer:
test.zig
const expect = @import("std").testing.expect;
test "address of syntax" {
// Get the address of a variable:
const x: i32 = 1234;
const x_ptr = &x;
// Dereference a pointer:
expect(x_ptr.* == 1234);
// When you get the address of a const variable, you get a const pointer to a single item.
expect(@TypeOf(x_ptr) == *const i32);
// If you want to mutate the value, you'd need an address of a mutable variable:
var y: i32 = 5678;
const y_ptr = &y;
expect(@TypeOf(y_ptr) == *i32);
y_ptr.* += 1;
expect(y_ptr.* == 5679);
}
test "pointer array access" {
// Taking an address of an individual element gives a
// pointer to a single item. This kind of pointer
// does not support pointer arithmetic.
var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const ptr = &array[2];
expect(@TypeOf(ptr) == *u8);
expect(array[2] == 3);
ptr.* += 1;
expect(array[2] == 4);
}
$ zig test test.zig
1/2 test "address of syntax"... OK
2/2 test "pointer array access"... OK
All 2 tests passed.
In Zig, we generally prefer Slices rather than Sentinel-Terminated Pointers. You can turn an array or pointer into a slice using slice syntax.
Slices have bounds checking and are therefore protected against this kind of undefined behavior. This is one reason we prefer slices to pointers.
test.zig
const expect = @import("std").testing.expect;
test "pointer slicing" {
var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
const slice = array[2..4];
expect(slice.len == 2);
expect(array[3] == 4);
slice[1] += 1;
expect(array[3] == 5);
}
$ zig test test.zig
1/1 test "pointer slicing"... OK
All 1 tests passed.
Pointers work at compile-time too, as long as the code does not depend on an undefined memory layout:
test.zig
const expect = @import("std").testing.expect;
test "comptime pointers" {
comptime {
var x: i32 = 1;
const ptr = &x;
ptr.* += 1;
x += 1;
expect(ptr.* == 3);
}
}
$ zig test test.zig
1/1 test "comptime pointers"... OK
All 1 tests passed.
To convert an integer address into a pointer, use @intToPtr
. To convert a pointer to an integer, use @ptrToInt
:
test.zig
const expect = @import("std").testing.expect;
test "@ptrToInt and @intToPtr" {
const ptr = @intToPtr(*i32, 0xdeadbee0);
const addr = @ptrToInt(ptr);
expect(@TypeOf(addr) == usize);
expect(addr == 0xdeadbee0);
}
$ zig test test.zig
1/1 test "@ptrToInt and @intToPtr"... OK
All 1 tests passed.
Zig is able to preserve memory addresses in comptime code, as long as the pointer is never dereferenced:
test.zig
const expect = @import("std").testing.expect;
test "comptime @intToPtr" {
comptime {
// Zig is able to do this at compile-time, as long as
// ptr is never dereferenced.
const ptr = @intToPtr(*i32, 0xdeadbee0);
const addr = @ptrToInt(ptr);
expect(@TypeOf(addr) == usize);
expect(addr == 0xdeadbee0);
}
}
$ zig test test.zig
1/1 test "comptime @intToPtr"... OK
All 1 tests passed.
See also:
volatile
Loads and stores are assumed to not have side effects. If a given load or store should have side effects, such as Memory Mapped Input/Output (MMIO), use volatile
. In the following code, loads and stores with mmio_ptr
are guaranteed to all happen and in the same order as in source code:
test.zig
const expect = @import("std").testing.expect;
test "volatile" {
const mmio_ptr = @intToPtr(*volatile u8, 0x12345678);
expect(@TypeOf(mmio_ptr) == *volatile u8);
}
$ zig test test.zig
1/1 test "volatile"... OK
All 1 tests passed.
Note that volatile
is unrelated to concurrency and Atomics. If you see code that is using volatile
for something other than Memory Mapped Input/Output, it is probably a bug.
To convert one pointer type to another, use @ptrCast. This is an unsafe operation that Zig cannot protect you against. Use @ptrCast
only when other conversions are not possible.
test.zig
const std = @import("std");
const expect = std.testing.expect;
test "pointer casting" {
const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 };
const u32_ptr = @ptrCast(*const u32, &bytes);
expect(u32_ptr.* == 0x12121212);
// Even this example is contrived - there are better ways to do the above than
// pointer casting. For example, using a slice narrowing cast:
const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0];
expect(u32_value == 0x12121212);
// And even another way, the most straightforward way to do it:
expect(@bitCast(u32, bytes) == 0x12121212);
}
test "pointer child type" {
// pointer types have a `child` field which tells you the type they point to.
expect(@typeInfo(*u32).Pointer.child == u32);
}
$ zig test test.zig
1/2 test "pointer casting"... OK
2/2 test "pointer child type"... OK
All 2 tests passed.
Alignment
Each type has an alignment - a number of bytes such that, when a value of the type is loaded from or stored to memory, the memory address must be evenly divisible by this number. You can use @alignOf to find out this value for any type.
Alignment depends on the CPU architecture, but is always a power of two, and less than 1 << 29
.
In Zig, a pointer type has an alignment value. If the value is equal to the alignment of the underlying type, it can be omitted from the type:
test.zig
const std = @import("std");
const expect = std.testing.expect;
test "variable alignment" {
var x: i32 = 1234;
const align_of_i32 = @alignOf(@TypeOf(x));
expect(@TypeOf(&x) == *i32);
expect(*i32 == *align(align_of_i32) i32);
if (std.Target.current.cpu.arch == .x86_64) {
expect(@typeInfo(*i32).Pointer.alignment == 4);
}
}
$ zig test test.zig
1/1 test "variable alignment"... OK
All 1 tests passed.
In the same way that a *i32
can be coerced to a *const i32
, a pointer with a larger alignment can be implicitly cast to a pointer with a smaller alignment, but not vice versa.
You can specify alignment on variables and functions. If you do this, then pointers to them get the specified alignment:
test.zig
const expect = @import("std").testing.expect;
var foo: u8 align(4) = 100;
test "global variable alignment" {
expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
expect(@TypeOf(&foo) == *align(4) u8);
const as_pointer_to_array: *[1]u8 = &foo;
const as_slice: []u8 = as_pointer_to_array;
expect(@TypeOf(as_slice) == []align(4) u8);
}
fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
fn noop1() align(1) void {}
fn noop4() align(4) void {}
test "function alignment" {
expect(derp() == 1234);
expect(@TypeOf(noop1) == fn() align(1) void);
expect(@TypeOf(noop4) == fn() align(4) void);
noop1();
noop4();
}
$ zig test test.zig
1/2 test "global variable alignment"... OK
2/2 test "function alignment"... OK
All 2 tests passed.
If you have a pointer or a slice that has a small alignment, but you know that it actually has a bigger alignment, use @alignCast to change the pointer into a more aligned pointer. This is a no-op at runtime, but inserts a safety check:
test.zig
const std = @import("std");
test "pointer alignment safety" {
var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
const bytes = std.mem.sliceAsBytes(array[0..]);
std.testing.expect(foo(bytes) == 0x11111111);
}
fn foo(bytes: []u8) u32 {
const slice4 = bytes[1..5];
const int_slice = std.mem.bytesAsSlice(u32, @alignCast(4, slice4));
return int_slice[0];
}
$ zig test test.zig
1/1 test "pointer alignment safety"... incorrect alignment
/home/andy/Downloads/zig/docgen_tmp/test.zig:10:63: 0x206f2b in foo (test)
const int_slice = std.mem.bytesAsSlice(u32, @alignCast(4, slice4));
^
/home/andy/Downloads/zig/docgen_tmp/test.zig:6:27: 0x2059ba in test "pointer alignment safety" (test)
std.testing.expect(foo(bytes) == 0x11111111);
^
/home/andy/Downloads/zig/lib/std/special/test_runner.zig:61:28: 0x22dd31 in std.special.main (test)
} else test_fn.func();
^
/home/andy/Downloads/zig/lib/std/start.zig:334:37: 0x20748d in std.start.posixCallMainAndExit (test)
const result = root.main() catch |err| {
^
/home/andy/Downloads/zig/lib/std/start.zig:162:5: 0x2071c2 in std.start._start (test)
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
^
error: the following test command crashed:
docgen_tmp/zig-cache/o/8fe80128a94c1613e48317295fc76cfc/test
allowzero
This pointer attribute allows a pointer to have address zero. This is only ever needed on the freestanding OS target, where the address zero is mappable. If you want to represent null pointers, use Optional Pointers instead. Optional Pointers with allowzero
are not the same size as pointers. In this code example, if the pointer did not have the allowzero
attribute, this would be a Pointer Cast Invalid Null panic:
allowzero.zig
const std = @import("std");
const expect = std.testing.expect;
test "allowzero" {
var zero: usize = 0;
var ptr = @intToPtr(*allowzero i32, zero);
expect(@ptrToInt(ptr) == 0);
}
$ zig test allowzero.zig
1/1 test "allowzero"... OK
All 1 tests passed.
Sentinel-Terminated Pointers
The syntax [*:x]T
describes a pointer that has a length determined by a sentinel value. This provides protection against buffer overflow and overreads.
test.zig
const std = @import("std");
// This is also available as `std.c.printf`.
pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
pub fn main() anyerror!void {
_ = printf("Hello, world!\n"); // OK
const msg = "Hello, world!\n";
const non_null_terminated_msg: [msg.len]u8 = msg.*;
_ = printf(&non_null_terminated_msg);
}
$ zig build-exe test.zig -lc
./docgen_tmp/test.zig:11:17: error: expected type '[*:0]const u8', found '*const [14]u8'
_ = printf(&non_null_terminated_msg);
^
./docgen_tmp/test.zig:11:17: note: destination pointer requires a terminating '0' sentinel
_ = printf(&non_null_terminated_msg);
^
See also: