Last active
December 18, 2015 16:29
-
-
Save jld/5812230 to your computer and use it in GitHub Desktop.
Obj.magic: not even once.
This file contains hidden or 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
| (* | |
| * Uses Obj.magic at 'a -> int, once; the rest is safe. When compiled with | |
| * ocamlopt, should fail with a segmentation fault or bus error at some point, | |
| * details depending on CPU. | |
| *) | |
| open Printf;; | |
| let () = | |
| let x = [|0|] in | |
| let xi : int = Obj.magic x in | |
| printf "xi = %d\n%!" xi; | |
| printf "xi lor 0 = %d\n%!" (xi lor 0); | |
| printf "xi - (xi lor 0) = %d\n%!" (xi - (xi lor 0)); | |
| (printf "[|0|].(xi - (xi lor 0)) = %!"; | |
| printf "0x%x\n%!" ([|0|].(xi - (xi lor 0)))); | |
| printf "(let a = [|0|] in a.(xi - (xi lor 0)) <- -1; Array.length a) = 0x%x\n%!" | |
| (let a = [|0|] in a.(xi - (xi lor 0)) <- -1; Array.length a); | |
| (printf "(let a = [|0|] in a.(xi - (xi lor 0)) <- -1; a.(pred (Array.length a))) = %!"; | |
| printf "0x%x\n" (let a = [|0|] in a.(xi - (xi lor 0)) <- -1; a.(pred (Array.length a)))) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative last expression: