title: Why does GCC allocate separate stack space for local unions in different scopes?
body: <p>Consider the following code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  
  #ifndef TRY
  #define TRY struct
  #endif
  
  TRY testme
  {
    int one;
    int two;
    char three;
    int four;
  };
  
  int
  main (void)
  {
    {
      volatile TRY testme one;
  
      one.one = 2;
      one.three = 7;
    }
  
    {
      volatile TRY testme twos;
  
      twos.one = 3;
    }
  
    {
      volatile TRY testme one;
  
      one.one = 4;
    }
  
    {
      volatile TRY testme twos;
  
      twos.one = 5;
    }
  
    {
      volatile TRY testme twos;
  
      twos.one = 6;
    }
  
    {
      volatile TRY testme twos;
  
      twos.one = 6;
    }
  
    return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>Compiled as is for x86 is (meaning testme is a struct), the stack size the compiler allocates for main is 16 bytes.</p>
  
  <pre><code>$ gcc -g -O2 test.c -o test 
  $ objdump -d ./test | ./checkstack.pl i386 | grep main
  16 main
  </code></pre>
  
  <p>However, compiled with TRY defined to union (meaning testme is a union), the stack size the compiler allocates for main is 32 bytes:</p>
  
  <pre><code>$ gcc -DTRY=union -g -O2 test.c -o test 
  $ objdump -d ./test | ./checkstack.pl i386 | grep main
  </code></pre>
  
  <p>Moreover, any additional instances of the struct/union defined in additional scopes, will produce bigger stack allocations when using a union, but will not enlarge the stack allocation when  used as a struct.</p>
  
  <p>Now, this does not make sense - the union should take less stack space, if at all, not more, then a struct with the same fields!</p>
  
  <p>It seems as if GCC treats unions as used concurrently even when in different scopes, but does not do the same for structs.</p>
  
  <p>Some more clarifications:</p>
  
  <ol>
  <li><p>volatile is used to stop the compiler from optimizing away the assignments. Loosing the volatile and compiling with no optimization produces the same results.</p></li>
  <li><p>Even if testme is a struct that has a union as one of the members, the same behavior is observed. In  other words - it is enough that one of the members of a struct is a union for GCC to for separate stack allocations.</p></li>
  <li><p>Compiler is gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) but other GCC versions for other architectures showed the same behavior.</p></li>
  <li><p>checkstack.pl simply searches the objdump output for the instructions used to allocate stack (sub to the stack pointer).</p></li>
  </ol>
  
  <p>My question:</p>
  
  <ol>
  <li>Why does GCC do this? is this a bug or is there a reason for this behavior?</li>
  <li>Assuming this is not a bug, is a there a way to work around this and force GCC to allocate stack for stucts same as unions.</li>
  </ol>
  
  <p><strong>Clarification</strong>: My question is not why the struct or union appears to be bigger in size from the size of its part. I understand the reason is padding for alignment. My problem is that the compiler allocates multiple stack frames for different instances of the union even though they are defined in different scopes while it shouldn't and indeed doesn't do the same for a struct with the same fields.</p>
  
  <p>Thanks!</p>