the Problem of Distribution memory of MATLAB struct?

5 次查看(过去 30 天)
clear all
clc
testStruct.a=int8(10);
a=int8(10)
whos testStruct
whos a
the result is:
a =
10
Name Size Bytes Class Attributes
testStruct 1x1 125 struct
Name Size Bytes Class Attributes
a 1x1 1 int8
Variable a takes up 8 bytes, however, testStruct.a takes up 125 bytes。why? my email:dengshuaiqi@163.com,think you very much。
  1 个评论
James Tursa
James Tursa 2016-7-1
编辑:James Tursa 2016-7-1
The whos result is misleading for this comparison. For the following discussion the numbers cited assume a 64-bit system and that the overhead for a MATLAB variable is 124 bytes (this is approx).
First, every MATLAB variable has an overhead structure called an mxArray that contains information such as class, size, data pointers, sharing pointers, etc. So for variable "a" above, the data for "a" is only showing 1 byte, but the mxArray for this variable is taking up an additional 124 bytes of memory. So the total amount of memory actually used by variable "a" is 125 bytes. (actually probably more due to memory alignment effects)
For the struct testStruct, it too has this mxArray overhead. So there is 124 bytes right there. Then the data area of this struct is actually mxArray pointers, another 8 bytes. Then there is memory for the field name "a", so another 2 bytes minimum for that. Then the data itself is actually a MATLAB variable, so the overhead and data for that come to another 125 bytes. So the total for the struct testStruct is actually 124 + 8 + 2 + 125 = 259 bytes.
Bottom line is that you can't really compare the whos result of structs to numeric variables since whos does not tell you the whole memory story, it only reports on the data areas.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2016-6-4
The structure as a whole takes up 125 bytes, not just the .a field of the structure.
The structure includes the overhead to list all of the field names and the types and sizes of each of the fields.
  6 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by