Pointers in Crystal

Here’s the translated code and explanation in Crystal, formatted in Markdown suitable for Hugo:

Crystal supports pointers, allowing you to pass references to values and records within your program.

def zeroval(ival : Int32)
  ival = 0
end

def zeroptr(iptr : Pointer(Int32))
  iptr.value = 0
end

i = 1
puts "initial: #{i}"

zeroval(i)
puts "zeroval: #{i}"

zeroptr(pointerof(i))
puts "zeroptr: #{i}"

puts "pointer: #{pointerof(i)}"

We’ll show how pointers work in contrast to values with two functions: zeroval and zeroptr. zeroval has an Int32 parameter, so arguments will be passed to it by value. zeroval will get a copy of ival distinct from the one in the calling function.

zeroptr in contrast has a Pointer(Int32) parameter, meaning that it takes an Int32 pointer. The iptr.value code in the function body then dereferences the pointer from its memory address to the current value at that address. Assigning a value to a dereferenced pointer changes the value at the referenced address.

The pointerof(i) syntax gives the memory address of i, i.e., a pointer to i.

Pointers can be printed too.

When you run this program, you’ll see output similar to:

initial: 1
zeroval: 1
zeroptr: 0
pointer: 0x7fff5fbff7f8

zeroval doesn’t change the i in the main function, but zeroptr does because it has a reference to the memory address for that variable.

Note that in Crystal, unlike some other languages, you don’t need to explicitly allocate or free memory when working with pointers. Crystal’s garbage collector handles memory management automatically.