Weird struct packing
<p>While working on some frames structure, I came across a  weird behavior.
I quickly tested it in and independent sample code with following:</p>

<pre><code>struct non_alligned_struct
{
    uint8_t  flag;

    // start of uint32 bit fields
    uint32_t a:2;
    uint32_t b:2;
    uint32_t c:1;
    uint32_t d:1;
    uint32_t e:1;
    uint32_t f:1;
    uint32_t g:3;
    uint32_t h:1;
    uint32_t i:1;
    uint32_t j:3;
    uint32_t k:3;
    uint32_t l:1;
    uint32_t m:1;
    uint32_t n:1;
    uint32_t o:1;
    uint32_t p:3;
    uint32_t q:2;
    uint32_t r:1;
    uint32_t s:1;
    uint32_t t:2;
    //4 bytes ends here

    // shouldn't this start at 5th byte ??
    uint16_t u;

    uint16_t v:13;
    uint16_t w:3;

    uint16_t x;

    uint16_t y:13;
    uint16_t z:3;
};

int main()
{
    struct non_alligned_struct obj1;
    void *ptr1 = &amp;obj1;
    void *ptr2 = &amp;(obj1.u);
    printf("ptr1: %p, ptr2: %p, ptr2 - ptr1: %d\n", ptr1, ptr2, ptr2 - ptr1);
    return 0;
}
</code></pre>

<p>Output:
ptr1: 0x7fff3216a620, ptr2: 0x7fff3216a626, ptr2 - ptr1: 6</p>

<p>Question: Why ptr2 - ptr1 should be 6. As per my calculation it should have been 5. Also the Structure is 13 bytes, so it gets 4 bytes aligned and the padding is done at weird way. I verified by giving random values to member variables and I observe that padding is done at following <strong>bold locations</strong></p>

<p>00000000: 01 22 31 10 67 <strong>00</strong> fe ff 86 01 fe ff 86 01 <strong>00 00</strong></p>