Version: 1.1.0

Using pointers

Using a pointer means allocating, initializing, indexing, and freeing in matching pairs.

Essentials

  • alloc<a href="/n/">T</a> reserves room for n values.
  • Store a value with ptr.init_pointee_move or assignment when the memory is already live.
  • Pointer arithmetic steps in units of T, not bytes.
  • bitcast reinterprets the address. It does not convert the value.
  • Never free a pointer you did not allocate, and never free it twice.

Example

fn fill(ptr: UnsafePointer[Int], n: Int):
    for i in range(n):
        ptr[i] = i

Keep in mind

Pass a Span across function boundaries when the length matters. A bare pointer forgets how much memory is live.