필터 지우기
필터 지우기

the Problem of Distribution memory of MATLAB struct?

조회 수: 5 (최근 30일)
deng
deng 2016년 6월 4일
편집: James Tursa 2016년 7월 1일
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
deng
deng 2016년 6월 6일
I see.Think you very much.
Walter Roberson
Walter Roberson 2016년 7월 1일
deng comments to my original Answer:
hello, problem of memmapfile function. think you very muchu. http://www.mathworks.com/matlabcentral/answers/293260-problem-of-memmapfile-function

댓글을 달려면 로그인하십시오.

추가 답변 (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