Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will MATLAB 'move' or ''copy return value?

Tags:

matlab

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?

like image 489
Joe C Avatar asked Mar 16 '16 16:03

Joe C


1 Answers

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.

like image 143
Ander Biguri Avatar answered Oct 05 '22 03:10

Ander Biguri