The addr operator

The addr operator returns the address of an l-value. If the type of the location is T, the addr operator result is of the type ptr T. An address is always an untraced reference. Taking the address of an object that resides on the stack is unsafe, as the pointer may live longer than the object on the stack and can thus reference a non-existing object. One can get the address of variables, but one can’t use it on variables declared through let statements:

  1. let t1 = "Hello"
  2. var
  3. t2 = t1
  4. t3 : pointer = addr(t2)
  5. echo repr(addr(t2))
  6. # --> ref 0x7fff6b71b670 --> 0x10bb81050"Hello"
  7. echo cast[ptr string](t3)[]
  8. # --> Hello
  9. # The following line doesn't compile:
  10. echo repr(addr(t1))
  11. # Error: expression has no address