English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: [Caml-list] ocaml sefault in bytecode: unanswered questions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-08-08 (14:15)
From: ivan chollet <ivan.chollet@f...>
Subject: RE: [Caml-list] ocaml sefault in bytecode: unanswered questions
Yes, makes sense. But it would mean that "List.iter myfun !myref" is not semantically equivalent to "List.iter myfun (let l=!myref in l)", therefore the expressions "let l=!myref in l" and "!myref" are not semantically equivalent.

That would be very strange!

-----Original Message-----
From: Edgar Friendly [mailto:thelema314@gmail.com] 
Sent: samedi 8 août 2009 15:29
To: ivan chollet
Cc: 'Cedric Auger'; caml-list@yquem.inria.fr
Subject: Re: [Caml-list] ocaml sefault in bytecode: unanswered questions

ivan chollet wrote:
> You basically state that stuff like “let l = !myref in List.iter myfun l”
> (where myfun modifies !myref) wouldn't produce segfault. 
> Why? It would mean that doing "let l = !myref" creates a brand new OCaml
> entity, l, ie a brand new allocation on the heap, and then, messing around
> with !myref inside myfun would be of course 100% safe. Is that what OCaml
> runtime does?

when you have [myref : list ref = ref [1;2]], this is in memory as:

myref -> {contents: _a}
_a -> (1,_b)
_b -> (2,0)

I've given names to the intermediate pointers -- the notation above
shows names as pointers to data structures.  [myref] is a pointer to a
record, whose contents point to the head of the list [1;2].  So there's
already two levels of indirection.

When you do [let l = !myref], [l] gets assigned [_a], so it points to
the head of the list.  List.iter traverses the list not even knowing
that [myref] exists, so if the function [myfun] modifies [myref], it
won't affect List.iter.

Does this make sense?