Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does GCC allocate separate stack space for local unions in different scopes?

Consider the following code:

#include <stdlib.h>

#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;
}

Compiled as is for x86 is (meaning testme is a struct), the stack size the compiler allocates for main is 16 bytes.

$ gcc -g -O2 test.c -o test 
$ objdump -d ./test | ./checkstack.pl i386 | grep main
16 main

However, compiled with TRY defined to union (meaning testme is a union), the stack size the compiler allocates for main is 32 bytes:

$ gcc -DTRY=union -g -O2 test.c -o test 
$ objdump -d ./test | ./checkstack.pl i386 | grep main

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.

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!

It seems as if GCC treats unions as used concurrently even when in different scopes, but does not do the same for structs.

Some more clarifications:

  1. volatile is used to stop the compiler from optimizing away the assignments. Loosing the volatile and compiling with no optimization produces the same results.

  2. 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.

  3. Compiler is gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) but other GCC versions for other architectures showed the same behavior.

  4. checkstack.pl simply searches the objdump output for the instructions used to allocate stack (sub to the stack pointer).

My question:

  1. Why does GCC do this? is this a bug or is there a reason for this behavior?
  2. 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.

Clarification: 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.

Thanks!

like image 290
gby Avatar asked Feb 22 '12 07:02

gby


1 Answers

Apparently at least an attempt has been made to relax gcc's strict aliasing paranoia regarding unions.

You may wish to make sure that gcc source you compile from has this or equivalent patch applied: http://codereview.appspot.com/4444051/

like image 178
atal Avatar answered Oct 14 '22 05:10

atal