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 08:34:04 +0000 (UTC)
Organization: PhysioSonics Inc
Lines: 26
Message-ID: <ggoads$sqr$1@fred.mathworks.com>
References: <g68b41$cu8$1@fred.mathworks.com> <muyd4l35v1v.fsf@G99-Boettcher.llan.ll.mit.edu>
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 1227861244 29531 172.30.248.37 (28 Nov 2008 08:34:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 28 Nov 2008 08:34:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1129061
Xref: news.mathworks.com comp.soft-sys.matlab:503608


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!