I recently started looking at some ctypes issues. I dug a bit into <a href="http://bugs.python.org/issue6069" target="_blank">http://bugs.python.org/issue6069</a> and then I found <a href="http://bugs.python.org/issue11920" target="_blank">http://bugs.python.org/issue11920</a>. They both boil down to the fact that bitfield allocation is up to the compiler, which is different in GCC and MSVC. Currently we have hard-coded allocation strategy based on paltform in cfields.c:<div>

<br><div><span style="font-family:monospace;line-height:17px;white-space:pre-wrap">&gt;    if (bitsize /* this is a bitfield request */</span></div><div><span style="font-family:monospace;line-height:17px"><pre style="white-space:pre-wrap;word-wrap:break-word">
&gt;        &amp;&amp; *pfield_size /* we have a bitfield open */
&gt; #ifdef MS_WIN32
&gt;        /* MSVC, GCC with -mms-bitfields */
&gt;        &amp;&amp; dict-&gt;size * 8 == *pfield_size
&gt; #else
&gt;        /* GCC */
&gt;        &amp;&amp; dict-&gt;size * 8 &lt;= *pfield_size
&gt; #endif
&gt;        &amp;&amp; (*pbitofs + bitsize) &lt;= *pfield_size) {
&gt;        /* continue bit field */
&gt;        fieldtype = CONT_BITFIELD;
&gt; #ifndef MS_WIN32
&gt;    } else if (bitsize /* this is a bitfield request */
&gt;        &amp;&amp; *pfield_size /* we have a bitfield open */
&gt;        &amp;&amp; dict-&gt;size * 8 &gt;= *pfield_size
&gt;        &amp;&amp; (*pbitofs + bitsize) &lt;= dict-&gt;size * 8) {
&gt;        /* expand bit field */
&gt;        fieldtype = EXPAND_BITFIELD;
&gt; #endif</pre></span><div>So when creating a bitfield structure, it&#39;s size can be different on Linux vs Windows.</div><div><br></div><div><div><font face="&#39;courier new&#39;, monospace" size="1">class MyStructure(ctypes.BigEndianStructure):</font></div>

<div><font face="&#39;courier new&#39;, monospace" size="1">    _pack_      = 1    # aligned to 8 bits, not ctypes default of 32</font></div><div><font face="&#39;courier new&#39;, monospace" size="1">    _fields_    = [</font></div>

<div><font face="&#39;courier new&#39;, monospace" size="1">                   (&quot;Data0&quot;,   ctypes.c_uint32, 32),</font></div><div><font face="&#39;courier new&#39;, monospace" size="1">                   (&quot;Data1&quot;,   ctypes.c_uint8, 3),</font></div>

<div><font face="&#39;courier new&#39;, monospace" size="1">                   (&quot;Data2&quot;,   ctypes.c_uint16, 12),</font></div><div><font face="&#39;courier new&#39;, monospace" size="1">                  ]</font></div>

</div><div><br></div><div>sizeof for above structure is 6 on GCC build and 7 on MSVC build. This leads to some confusion and issues, because we can&#39;t always interop correctly with code compiled with different compiler than the one Python is compiled with on the platform. Short term solution is to add a warning in the documentation about this. Longer term though, I think it would be better to add a property on the Structure class for configurable allocation strategy, for example Native (default), GCC, MSVC and when allocating the bitfield, use given strategy. Native would behave similar to what happens now, but we would also allow GCC-style allocation on Windows for example and the ability to extend this if we ever run into similar issues with other compilers. This shouldn&#39;t be too difficult to implement, will be backwards compatible and it would improve interop. I would like to hear some opinions on this.</div>

</div></div><div><br></div><div>Thank you,</div><div>Vlad</div>