Dos varargout make a copy of the output variables?
Show older comments
I'm trying to optimize some code for memory use and I'm wondering if the varargout variable makes a copy of the variables which are assigned to it. For example:
function varargout = foo(bar)
C={rand(bar,bar^2,bar^3), false(100,100,100))};
varargout(1:nargout)=C(1:nargout);
end
If bar is a big number than C will be huge and if a copy needs to be made at any stage an out of memory error may occur. My gut tells me no copy is made but I can't seem to find an explicit answer.
Accepted Answer
More Answers (1)
Walter Roberson
on 19 Mar 2018
0 votes
MATLAB is a copy-on-write system. When a copy of the contents of C is required by the assignment to varargout, the internal pointers to the entries are copied and the reference count of the nodes are incremented.
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!