Skip to content

Instantly share code, notes, and snippets.

@Th0rgal
Last active April 30, 2022 16:47
Show Gist options
  • Save Th0rgal/4e3aabbcecdde04eb32b912a8ec33491 to your computer and use it in GitHub Desktop.
Save Th0rgal/4e3aabbcecdde04eb32b912a8ec33491 to your computer and use it in GitHub Desktop.
Demonstration of array usage in cairo
%builtins output
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.serialize import serialize_word
func print_array{output_ptr : felt*}(res_len : felt, res : felt*):
if res_len == 0:
return ()
end
serialize_word([res])
print_array(res_len - 1, res + 1)
return ()
end
func fill_array(arr : felt*, size : felt) -> ():
if size == 0:
return ()
else:
assert [arr] = size
return fill_array(arr + 1, size - 1)
end
end
func main{output_ptr : felt*}():
const FILL_SIZE = 2
alloc_locals
# Allocate an array.
let (ptr) = alloc()
# Populate some values in the array.
assert [ptr] = 32
assert [ptr + 1] = 64
# fill the array
fill_array(arr=ptr + 2, size=FILL_SIZE)
# the line that requires alloc_locals because ptr was revoked
print_array(2 + FILL_SIZE, ptr)
return ()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment