title: Size of Structs in Objective-C and C++
body: <p>I am converting some Objective-C++ code into plain Objective-C and I am having some trouble with structs.  For both languages, I have the struct declared in the .h file like this.</p>
  
  <pre><code>struct BasicNIDSHeader {
      short messageCode;
      short messageDate;
      int messageTime;
      int messageLength;
      short sourceID;
      short destID;
      short numberOfBlocks;
  };
  </code></pre>
  
  <p>In C++, the struct is being declared like</p>
  
  <pre><code>BasicNIDSHeader header;
  </code></pre>
  
  <p>and in Objective-C I do this</p>
  
  <pre><code>struct BasicNIDSHeader header;
  </code></pre>
  
  <p>The code for using them is actually the same in both languages.</p>
  
  <pre><code>memset(&amp;header, 0, sizeof(header));
  [[fileHandle readDataOfLength:sizeof(header)] getBytes:&amp;header];
  </code></pre>
  
  <p>where fileHandle is a NSFileHandle.</p>
  
  <p>The problem is than in the original C++ code, sizeof(header) = 18. When using Objective-C,  sizeof(header) = 20.</p>
  
  <p>Any ideas why this is happening or how to fix it?  The code is dependent on the size being like it is in C++.  I can just hardcode it, but would like to have a better understanding of why it is happening.  Plus I hate hardcoding constants.</p>
  
  <p>Thanks!</p>