Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: copy-on-write with MEX files
Date: Fri, 28 Nov 2008 09:04:02 +0000 (UTC)
Organization: PhysioSonics Inc
Lines: 31
Message-ID: <ggoc62$8p1$1@fred.mathworks.com>
References: <g68b41$cu8$1@fred.mathworks.com> <muyd4l35v1v.fsf@G99-Boettcher.llan.ll.mit.edu> <ggoads$sqr$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1227863042 8993 172.30.248.37 (28 Nov 2008 09:04:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 28 Nov 2008 09:04:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1129061
Xref: news.mathworks.com comp.soft-sys.matlab:503611


"Ryan Ollos" <ryano@physiosonics.com> wrote in message <ggoads$sqr$1@fred.mathworks.com>...
> Peter Boettcher <boettcher@ll.mit.edu> wrote in message <muyd4l35v1v.fsf@G99-Boettcher.llan.ll.mit.edu>...
> 
> > To expand on this a little, there is a C-level function in MATLAB that
> > "unshares" a variable.  Internal functions in MATLAB that write to
> > variables (subsasgn, etc) call this function before writing to the
> > memory.
> 
> Maybe you can help me clarify how this works.  Suppose the following:
> 
> X = ones(10, 1);
> Y = X;
> Z= X;
> X(1) = 2;
> 
> Based on my understanding and the tests I have done, it seems like the following happens:
> 
> Memory is allocated and X is set to point to address a1.
> Y is set to point to a1.
> Z is set to point to a1.
> A deep copy of the memory pointed by X, Y, and Z is performed (address a2), Y and Z are changed to point to a2, and a new value is assigned to the location pointed to by X(1).
> 
> However, I suppose its possible that the situation is reversed and X points to a2 while Y and Z continue to point to a1.  However, this seems like it would introduce a number of problems with sharing data between the MATLAB and MEX workspaces.
> 
> Can you clarify if the behaviour I have described is correct?
> 
> Thanks!

I found some info in another newsgroup post, where someone noted that you can type 'format debug' and get the memory address location printed to the terminal.

It seems to be the case for my example that Y and Z continue to point to a1, while X points to a2.  I suppose this is a computational advantage because it changes a fewer number of pointers.