11

Hello all,
I want to know whether union uses padding?
since the size of union is the largest data member size, can there be padding at the end?

timrau
  • 22,578
  • 4
  • 51
  • 64
akash
  • 1,801
  • 7
  • 24
  • 42
  • 1
    In short, **Yes**!!On my Windows system, a union is padded to have a size that is a multiple of the size of an `int`,ie **4 bytes**.So unless you use `#pragma pack(1)`, a union having size 3 bytes will be padded with one more byte to yield 4 bytes.Write a program and try it before posting questions. – Rüppell's Vulture May 25 '13 at 12:03
  • "a union is padded to have a size that is a multiple of the size of an int,ie 4 bytes." but in this case union ab{ char a; }a; the size is 1 – akash May 25 '13 at 12:13
  • 1
    In your case there is no padding at all.Who said `padding` is always done?Like Daniel Fischer said,it is not **mandated** by the standard. – Rüppell's Vulture May 25 '13 at 14:24
  • `union u{unsigned a:21;};` - This union is 4 bytes on my Windows system unless I use `#pragma pack(1)`,3 bytes if I use it. – Rüppell's Vulture May 25 '13 at 15:07

2 Answers2

16

since the size of union is the largest data member size

That need not be true. Consider

union Pad {
    char arr[sizeof (double) + 1];
    double d;
};

The largest member of that union is arr. But usually, a double will be aligned on a multiple of four or eight bytes (depends on architecture and size of double). On some architectures, that is even necessary since they don't support unaligned reads at all.

So sizeof (union Pad) is usually larger than sizeof (double) + 1 [typically 16 = 2 * sizeof (double) on 64-bit systems, and either 16 or 12 on 32-bit systems (on a 32-bit system with 8-bit char and 64-bit double, the required alignment for double may still be only four bytes)].

That means there must then be padding in the union, and that can only be placed at the end.

Generally, the size of a union will be the smallest multiple of the largest alignment required by any member that is not smaller than the largest member.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • but when we have char arr[sizeof(double)+1] alone in union its just gives size: 9? – Dineshkumar May 25 '13 at 13:31
  • That's not mandated by the standard, an implementation is allowed to add padding, but in general (with 8-byte `double`s), yes, that would give a size of 9. – Daniel Fischer May 25 '13 at 13:34
  • Did anyone manage to show that this example will pad up? - asking because I don't think there is _any_ reason to pad here, its not as if there needs to be space between the two members, it should just pick the largest one. – ideasman42 Mar 22 '15 at 16:07
  • 1
    @ideasman42 You can check whether your compiler inserts padding by writing and running a small programme printing out the sizes of `double` and `union Pad`. I get that the size of the union is twice the size of `double` (16 = 2*8) on a 64-bit system from both gcc and clang [as expected]. The reason for padding is alignment (both align `double` on eight-byte boundaries). – Daniel Fischer Mar 22 '15 at 16:32
  • Interesting, it **does** align, however if `double d` is replaced with `char d[sizeof(double)]`, then the union isn't padded. (linux/gcc at least) – ideasman42 Mar 22 '15 at 16:34
  • @ideasman42 Yes, since the alignment of `char` is 1. If you use `int d[2]`, you will probably get 12 as the size since `int` more often than not is four bytes with four byte alignment nowadays. (Except perhaps on embedded systems where it may be two bytes often.) – Daniel Fischer Mar 22 '15 at 16:37
-2

Union when we use with primitive types inside it

union
{
    char c;
    int x;
}

It seems like it doesn't use padding because the largest size data is anyway is aligned to boundary.

But when we nest a structure within union it does.

    union u
    {
        struct ss
        {
            char s;
            int v;
        }xx;
    }xu;

It resulted in size 8.

so answer for your question is PADDING is persent in UNION.

Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
  • 1
    As Daniel suggested this is wrong. You are referring padding of struct as padding of union which is misleading. – kadina Jul 11 '16 at 17:42