Last active
September 18, 2018 15:33
-
-
Save samth/40e8cebf2d8b0dc53671ffdff5ec7d61 to your computer and use it in GitHub Desktop.
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
[samth@huor:~/sw/plt (master) plt] r ~/tmp/ptr.rkt | |
diff = -104 | |
x = 7f6f077a0fd0, *x = 7 | |
y = 7f6f077a1038, *y = 5 | |
p = 7f6f077a0fd0, *p = 7 |
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
#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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that if we replace
diff
with-104
the result is unchanged.