Borrar filtros
Borrar filtros

the Problem of Distribution memory of MATLAB struct?

3 visualizaciones (últimos 30 días)
deng
deng el 4 de Jun. de 2016
Editada: James Tursa el 1 de Jul. de 2016
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 comentario
James Tursa
James Tursa el 1 de Jul. de 2016
Editada: James Tursa el 1 de Jul. de 2016
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.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Jun. de 2016
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 comentarios
deng
deng el 6 de Jun. de 2016
I see.Think you very much.
Walter Roberson
Walter Roberson el 1 de Jul. de 2016
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by