title: C++ The compiler is changing the alignment of my structures. How can I prevent this?
body: <p>I am writing some code to read bitmap files.</p>
  
  <p>Here is the struct I am using to read the bitmap header. See also:</p>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd183374(v=vs.85).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/windows/desktop/dd183374(v=vs.85).aspx</a></p>
  
  <pre><code>struct BITMAPFILEHEADER
  {
          WORD  bfType; // 2
          DWORD bfSize; // 6
          WORD  bfReserved1; // 8
          WORD  bfReserved2; // 10
          DWORD bfOffBits; // 14
  }; // should add to 14 bytes
  </code></pre>
  
  <p>If I put the following code in my main function:</p>
  
  <pre><code>std::cout &lt;&lt; "BITMAPFILEHEADER: " &lt;&lt; sizeof(BITMAPFILEHEADER) &lt;&lt; std::endl;
  </code></pre>
  
  <p>the program prints:</p>
  
  <pre><code>BITMAPFILEHEADER: 16
  </code></pre>
  
  <p>It appears to be re-aligning the data in the struct on 4-byte boundaries, presumably for efficiency. Of course this renders me unable to read a bitmap... Even though microsoft, and others, specifiy this is the way to do it...</p>
  
  <p>How can I prevent the structure re-alignment?</p>