Where was the empty array created?

2 views (last 30 days)
fa wu
fa wu on 20 Aug 2023
Edited: Stephen23 on 20 Aug 2023
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

Accepted Answer

Bruno Luong
Bruno Luong on 20 Aug 2023
Edited: Bruno Luong on 20 Aug 2023
"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 Comments
fa wu
fa wu on 20 Aug 2023
Got it ,Thank you very much!
Stephen23
Stephen23 on 20 Aug 2023
Edited: Stephen23 on 20 Aug 2023
"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."

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!