MATLAB: Order of variable deletion and creation

memory

Is the order in which the left hand side is deleted and the right hand side executed defined?
i.e.
output = createNewOutput();
If "output" exists, does it get deleted first, before createNewOutput runs? Or alternatively, does createNewOutput run before output is deleted? Is this behavior undefined?

Best Answer

If I understand you correctly, the output will not be deleted first.
You can easily test this yourself. Create the following function in your workspace:
function output = thisWillCrash()
this will crash
end
Then run the following code in your workspace:
output = 'I exist!';
output = thisWillCrash()
You will see that the original value of output still exists, because the new value was never generated, and never overwrote the old one.
Anticipating that you might have other memory-usage questions, let me refer you to some documentation about how MATLAB allocates memory.
Related Question