Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Last active April 5, 2021 14:58
Show Gist options
  • Save PhilipWitte/b4c8188287175cf99338 to your computer and use it in GitHub Desktop.
Save PhilipWitte/b4c8188287175cf99338 to your computer and use it in GitHub Desktop.
Unchecked Array vs Pointer Arithmatic
type
CArray{.unchecked.}[T] = ptr array[1, T]
type
A = object
items: ptr float
B = object
items: CArray[float]
proc getItem(a:A, i:int): float = cast[ptr float](cast[int](a.items) + (i * sizeof float))[]
proc getItem(b:B, i:int): float = b.items[i] # much easier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment