Skip to content

Instantly share code, notes, and snippets.

@jld
Last active December 18, 2015 16:29
Show Gist options
  • Select an option

  • Save jld/5812230 to your computer and use it in GitHub Desktop.

Select an option

Save jld/5812230 to your computer and use it in GitHub Desktop.
Obj.magic: not even once.
(*
* 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))))
@jld
Copy link
Author

jld commented Jun 19, 2013

Alternative last expression:

let a = [|0|] in a.(xi - (xi lor 0)) <- min_int; Gc.minor (); Array.length a

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