Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Declare the structure as a packed structure to avoid alignment issues;</p> <p>On windows use <code>#pragma pack (1)</code> (see <a href="http://msdn.microsoft.com/en-us/library/2e70t5y1%28v=vs.80%29.aspx" rel="nofollow">msdn</a>) </p> <p>On gcc use <code>__attribute__((packed))</code> </p> <p>to remove alignment problems. Actually gcc will support the windows style pragmas for compatibility. Have a look at:</p> <p><a href="http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html" rel="nofollow">http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html</a></p> <p>EDIT</p> <p>So example code below shows ONE packed structure inside another untouched structure:</p> <p>When compiled on x86_64-bit platform:</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdint.h&gt; #include &lt;string.h&gt; using namespace std; uint8_t input[] = { 0x00, 0x03, 0x23, 0x00, 0x57, 0x6f, 0x57, 0x00, 0x01, 0x0c, 0x01, 0xf3, 0x16, 0x36, 0x38, 0x78, 0x00, 0x6e, 0x69, 0x57, 0x00, 0x42, 0x47, 0x6e, 0x65, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x01, 0x05, 0x41, 0x44, 0x4d, 0x49, 0x4e }; struct __attribute__((packed)) packet{ uint8_t cmd; uint8_t error; uint16_t pkt_size; uint32_t gamename; uint8_t version1; uint8_t version2; uint8_t version3; uint16_t build; uint32_t platform; uint32_t os; uint32_t country; uint32_t timezone_bias; uint8_t ip[4]; uint8_t srp_I_len; uint8_t srp_I[16]; }; struct data { long int foo; short a; uint8_t b; struct packet p; uint32_t bar; }; int main() { struct packet p; struct data d; cout &lt;&lt; "in: " &lt;&lt; sizeof(input) &lt;&lt; ", d: " &lt;&lt; sizeof (d) &lt;&lt; ", p: " &lt;&lt; sizeof(p) &lt;&lt; " d.p: " &lt;&lt; sizeof(d.p) &lt;&lt; endl; memset(&amp;p, 0, sizeof(p)); memcpy(&amp;p, input, sizeof(input)); cout &lt;&lt; (int) p.srp_I_len &lt;&lt; endl; cout &lt;&lt; p.srp_I &lt;&lt; endl; } $./foo in: 39, d: 72, p: 50 d.p: 50 5 ADMIN </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload