Version française
Home     About     Download     Resources     Contact us    
Browse thread
Converting variants with only constant constructors to integers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Converting variants with only constant constructors to integers
Of course intify can cause a segmentation fault!

# let arr = Array.of_list [intify 1.0; 0];;
Segmentation fault

You can even have one with Obj.repr.

# let arr = Array.of_list [Obj.repr 1.0; Obj.repr 1];;
Segmentation fault

Only use these things once you understand the details of internal
representations...

Jacques Garrigue


On Tue, Jun 8, 2010 at 8:40 PM, bluestorm <bluestorm.dylc@gmail.com> wrote:
> You're right : that's true in the "int" case and I hadn't considered it.
> But if you use the same trick for ('a -> float) or other types you can get a
> segfault :
> # external floatify : 'a -> float = "%identity";;
> external floatify : 'a -> float = "%identity"
> # floatify 0;;
> Segmentation fault
> Also there are other problems with "intify" that may be worse than segfaults
> : your application running with nonsense data and failing at an arbitrary
> later point. Though ocaml doesn't fail (in the current implementation, it
> would be unwise to assume that the GC/runtime will never check this), you
> get strange behavior with those integers-wich-are-not-integers :
> # let x = intify (1,2);;
> val x : int = 70246988845964
> # x * 1 - x;;
> - : int = 1