How does MATLAB handle memory for structures and cell arrays?

How does MATLAB handle memory for structures and cell arrays?

 採用された回答

MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日

0 投票

Structures and cell arrays in MATLAB are implemented as arrays of pointers to other arrays. When a field or cell is changed only the pointer value for that cell or field is changed.
To produce the correct behavior (as if MATLAB had copied the values) MATLAB creates a shared data link with the array you place in the field. If you change part of that field later with a statement such as
a.data(3) = 5;
MATLAB does create a copy at that time (if the other array still exists).
The sequence of statements
r = rand(512);
a.data = r;
r = [];
a.data(3) = 5;
does all the work in place with no copies. On the other hand, the statements
r = rand(512);
a.data = r;
a.data(3) = 5;
will make a copy on the last statement.
Note that if your structure or cell array is linked to another, changing one field will require that the pointer array be copied (but not the data).

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by