Given the following code
function [out] = doit()
out = [1,2,3,4,5,6];
end
tmp = doit();
Will MATLAB copy the memory of out
to tmp
or move it directly to tmp
?
There is nothing like pointer in MATLAB, there are local workspaces.
It seems that MATLAB is smart and does a copy and return whenever needed and passes by "pointer" (without you knowing about it) if is not needed. As @Daniel and @zeeMonkeez pointed out, this is discussed in Loren's blog here, and she answers explicitly about function returns when memory has been created inside here. The general idea in those posts is "MATLAB will not copy unless its needed"
Basically MATLAB abstracts you from memory management.
Sidenote: There is something like pointers if you really need to exploit this. See @excaza's comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With