caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Edgar Friendly <thelema314@gmail.com>
To: ivan chollet <ivan.chollet@free.fr>
Cc: 'Cedric Auger' <Cedric.Auger@lri.fr>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] ocaml sefault in bytecode: unanswered questions
Date: Sat, 08 Aug 2009 09:29:19 -0400	[thread overview]
Message-ID: <4A7D7DAF.6070904@gmail.com> (raw)
In-Reply-To: <000301ca1809$1658efa0$430acee0$@chollet@free.fr>

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?

E


  reply	other threads:[~2009-08-08 13:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-08  9:17 ivan chollet
2009-08-08 13:29 ` Edgar Friendly [this message]
2009-08-08 14:15   ` ivan chollet
2009-08-08 17:14     ` David Allsopp
2009-08-09  1:45       ` ivan chollet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4A7D7DAF.6070904@gmail.com \
    --to=thelema314@gmail.com \
    --cc=Cedric.Auger@lri.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=ivan.chollet@free.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).