Skip to content

Instantly share code, notes, and snippets.

@samth
Last active September 18, 2018 15:33
Show Gist options
  • Save samth/40e8cebf2d8b0dc53671ffdff5ec7d61 to your computer and use it in GitHub Desktop.
Save samth/40e8cebf2d8b0dc53671ffdff5ec7d61 to your computer and use it in GitHub Desktop.
[samth@huor:~/sw/plt (master) plt] r ~/tmp/ptr.rkt
diff = -104
x = 7f6f077a0fd0, *x = 7
y = 7f6f077a1038, *y = 5
p = 7f6f077a0fd0, *p = 7
#lang racket
(require ffi/unsafe)
(define (addrof v) (cast v _racket _intptr))
(define x (box 3))
(define y (box 5))
(define xi (cast x _racket _intptr))
(define yi (cast y _racket _intptr))
(define diff (- xi yi))
(printf "diff = ~s\n" diff)
(define p (cast (+ yi diff) _intptr _racket))
(set-box! p 7)
(printf "x = ~x, *x = ~s\n" (addrof x) (unbox x))
(printf "y = ~x, *y = ~s\n" (addrof y) (unbox y))
(printf "p = ~x, *p = ~s\n" (addrof p) (unbox p))
@samth
Copy link
Author

samth commented Sep 18, 2018

Note that if we replace diff with -104 the result is unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment