title: .net object size padding?
body: <p><a href="http://msdn.microsoft.com/en-us/library/eahchzkf%28v=vs.100%29.aspx" rel="noreferrer">msdn</a> says that </p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>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 **.</p>
  </blockquote>
  
  <p>once ive read in the book : <em>c# via clr (page 522)</em></p>
  
  <p>that : 
  <img src="https://i.stack.imgur.com/HSyFi.jpg" alt="enter image description here"></p>
  
  <p>questions : </p>
  
  <p>1) does the padding mentioned in here :</p>
  
  <p><img src="https://i.stack.imgur.com/bROKO.jpg" alt="enter image description here"></p>
  
  <p>is the <strong>same</strong> as mentioned in the book ?</p>
  
  <p>AND</p>
  
  <p>2) if i have object type of <code>Person</code> - how can i know its <strong>TRUE SIZE</strong> in MEMORY ?</p>
  
  <h2>edit - why do i need that ?</h2>
  
  <p>please notice <a href="http://msdn.microsoft.com/en-us/library/dd997372.aspx" rel="noreferrer">this</a> : </p>
  
  <p>they have a sample of  reading records : </p>
  
  <pre><code> using (var accessor = mmf.CreateViewAccessor(offset, length))
              {
  
                  int colorSize = Marshal.SizeOf(typeof(MyColor)); //&lt;--------HERE
                  MyColor color;
  
  
                  for (long i = 0; i &lt; length; i += colorSize)
                  {
                      accessor.Read(i, out color);
                      color.Brighten(10);
                      accessor.Write(i, ref color);
                  }
              }
          }
  </code></pre>
  
  <p>if the size being reported by <code>MARSHAL.sizeOF</code> is <strong>not</strong> the size as <code>sizeOF</code> so - <strong>which</strong> should i choose ? it has to be accurate !! </p>
  
  <p>according to this sample , they dont consider the padding  , and they should... ( or not...)</p>