Skip to content

Instantly share code, notes, and snippets.

@kungfooman
Created June 19, 2017 06:38
Show Gist options
  • Save kungfooman/408fb93dd8887fe3b58e4c2664587108 to your computer and use it in GitHub Desktop.
Save kungfooman/408fb93dd8887fe3b58e4c2664587108 to your computer and use it in GitHub Desktop.
# julia -i readwritepointer.js
type keyboard_s
forward::Int32
backward::Int32
left::Int32
right::Int32
jump::Int32
end
address = ccall(:malloc, (Int64), (Int64, ), sizeof(keyboard_s))
ptr = Ptr{keyboard_s}(address)
# this code shall change the actual malloc()ed memory, but it basically just makes a copy instead
thekeyboard_a = unsafe_load(ptr)
thekeyboard_a.forward = 123 # this has no effect on the real memory address :(
# read the pointer again, same address, "forward" have the value 123
thekeyboard_b = unsafe_load(ptr)
if thekeyboard_a.forward == thekeyboard_b.forward
print("works!\n")
else
print("doesnt work!\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment