Last active
April 5, 2021 14:58
-
-
Save PhilipWitte/b4c8188287175cf99338 to your computer and use it in GitHub Desktop.
Unchecked Array vs Pointer Arithmatic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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