msdn says that
the sizeof operator can be used only in unsafe code blocks. Although you can use the Marshal.SizeOf method, the value returned by this method is not always the same as the value returned by sizeof.
and
Marshal.SizeOf returns the size after the type has been marshaled, whereas sizeof returns the size as it has been allocated by the common language runtime, including any ** padding **.
once ive read in the book : c# via clr (page 522)
that :
questions :
1) does the padding mentioned in here :
is the same as mentioned in the book ?
AND
2) if i have object type of Person
- how can i know its TRUE SIZE in MEMORY ?
edit - why do i need that ?
please notice this :
they have a sample of reading records :
using (var accessor = mmf.CreateViewAccessor(offset, length))
{
int colorSize = Marshal.SizeOf(typeof(MyColor)); //<--------HERE
MyColor color;
for (long i = 0; i < length; i += colorSize)
{
accessor.Read(i, out color);
color.Brighten(10);
accessor.Write(i, ref color);
}
}
}
if the size being reported by MARSHAL.sizeOF
is not the size as sizeOF
so - which should i choose ? it has to be accurate !!
according to this sample , they dont consider the padding , and they should... ( or not...)