Where was the empty array created?

3 ビュー (過去 30 日間)
fa wu
fa wu 2023 年 8 月 20 日
編集済み: Stephen23 2023 年 8 月 20 日
My understanding of classes: one of the roles of class properties is to store data.
When I run the following command,
x=Base.empty(7,0)
it shows that I've created a 7*0 Base array.
Qusetion:
1 So, is this 7*0 Base array stored in 'a,' ?in 'b'? or does 'a' and 'b' each have a separate 7*0 Base array? Why?
2 Is 'empty' a method or a function? This page shows it as a function. However, in OOP, aren't we supposed to use methods within classes? Why is 'empty' defined as a function and not a method?"
if you input mc=?Base; in command line , you will find mc.MethodList(3,1) is 'empty' method.
It seems like 'empty' is also a method, so is 'empty' actually a method or a function? Does MATLAB have the concept of class functions? If there are class functions, what is the difference between class functions and class methods?"
classdef Base
properties (Access=public)
a;
b;
end
methods
function obj=Base(value)
obj.a=value;
end
end
methods (Access=private)
function Fun(obj)
disp(num2str(obj.a));
end
end
end

採用された回答

Bruno Luong
Bruno Luong 2023 年 8 月 20 日
編集済み: Bruno Luong 2023 年 8 月 20 日
"1 So, is this 7*0 Base array stored in 'a,' ?in 'b'? or does 'a' and 'b' each have a separate 7*0 Base array? Why?"
This question has no sense to me.
The object (empty or not) never stored in its properties. And the second part of your question sounds like you see properties as object. They are not.
In this empty doc page one can read "empty is a hidden, public, static method of all nonabstract MATLAB® classes. You can override the empty method in class definitions."
So empty is a method.
Internally an array MATLAB objects has a meta data (mxArray structure) that stores the dimension and size of the array (among other thongs), and an array of data pointers that point to each internal representation element of the array. For empty array, one of the element of the size is 0 and the array pointer is NULL. It goes nowhere indicates there is NOTHING behind it.
There is not much sense to ask where an empty array is stored. It is not stored anywhere. Only the meta data remains and describing the class and size of the empty array.
  4 件のコメント
fa wu
fa wu 2023 年 8 月 20 日
Got it ,Thank you very much!
Stephen23
Stephen23 2023 年 8 月 20 日
編集済み: Stephen23 2023 年 8 月 20 日
"Does meta data occupy memory space? ... So, where is the metadata stored?"
The MATLAB documentation states "MATLAB also stores information about the array data, such as its class and dimensions, in a small, separate block of memory called a header."

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by