Memory position of elements in C/C++ union
<p>I have a union in C like this:</p>

<pre><code>union AUnion {
  struct CharBuf {
    char *buf;
    size_t len;
  } charbuf;
  uint8_t num;
  double fp_num;
};
</code></pre>

<p>My question is, can I guarantee that if given the following:</p>

<pre><code>union AUnion u;
</code></pre>

<p>Then the following are true:</p>

<pre><code>&amp;u == &amp;u.num
&amp;u == &amp;u.fp_num
&amp;u == &amp;u.charbuf
</code></pre>

<p>I.e they all start at the beginning of the memory segment where <code>u</code> is stored.</p>

<p>In the case of this C program compiled with <code>gcc version 5.3.0</code> and <code>-std=c11</code> the above is true:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdint.h&gt;
#include &lt;stdlib.h&gt;

union AUnion {
    struct CharBuf {
        char *buf;
        size_t len;
    } charbuf;
    uint8_t num;
    double fp_num;
};

int main(void)
{
    union AUnion u;
    printf("%d\n", ((void*)&amp;u) == ((void*)&amp;u.charbuf));
    printf("%d\n", ((void*)&amp;u.charbuf) == ((void*)&amp;u.num));
    printf("%d\n", ((void*)&amp;u.num) == ((void*)&amp;u.fp_num));
}
</code></pre>

<p>As it prints:</p>

<pre><code>1
1
1
</code></pre>

<p>Compiling the code above as C++11 with the same compiler results in the same output as compiling it as C11.</p>

<p>But is this standardized behaviour? Is it undefined? Can I rely on this behaviour with most C compilers? Can I expect this behaviour with C++ compilers as well?</p>