-5

code

#include <stdio.h>

union Data_u
{
    long x;
    char ch[20];
};

main()
{
    union Data_u data;
    printf("%ld\n", sizeof(data));
}

I should know what would be output in this code, but I think I don't understand how to calculate the size of the union properly. I did size of x(4 byte) + 20 = 24. But that is not correct. Can someone tell me where I am making a mistake?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ana Matijanovic
  • 277
  • 3
  • 13
  • 1
    Please post your code as text within the question. – cf- May 13 '16 at 22:44
  • 1
    Welcome to Stack Overflow. Please read the [About] page soon. When you post code, please include it in the question (rather than, or as well as, an off-site link). Please look up structure padding and alignment; it is highly unlikely that the gist of your question has not been answered multiple times here on SO. – Jonathan Leffler May 13 '16 at 22:45
  • (a) What result did you get? (b) Are you working on a 32-bit or 64-bit system? On a 32-bit system, you'd probably get the result 20; on a 64-bit system, you'd probably get the result 24, but not for the reason you gave. – Jonathan Leffler May 13 '16 at 22:50
  • 2
    That's not a structure, it's a union. And your question should state the actual result that you got. – user3386109 May 13 '16 at 22:50
  • I got result 24. And correct is 20. I will look on that question, sorry for duplicate. – Ana Matijanovic May 13 '16 at 22:52
  • This one is more likely to be helpful: [sizeof struct and sizeof union](https://stackoverflow.com/questions/22844542/sizeofstruct-and-sizeofunion) – user3386109 May 13 '16 at 22:59

2 Answers2

0

If you change your code to:

union Data_u
{
    long x;
    char ch[20];
} __attribute__((packed));

sizeof() will give you 20.

But you shouldn't do it. Let the compiler organize it the way it thinks it is optimal.

0

Structures lay out their members sequentially in memory, and it's meant to be meaningful to access any and all of their members. If you had :

struct Data_u
{
    long x;
    char ch[20];
};

Then you would expect to be able to access x and ch, independently and correctly. In general, the size of a structure is the size of the sum of its fields (plus padding), which looks like what you tried to calculate.

However, this is a union, not a structure. Unions are meant to represent just one of their fields, and you somehow decide which one it makes sense to use. The other members should be considered invalid. Because only one member is meant to be valid at a time, it would be wasteful to have them laid out sequentially in memory, so in most cases every member of a union is aligned at the start of the union in memory. This means that in general, the size of a union is the size of its largest member (plus padding).

In your case, the largest member is the char array, independent of bitness. In a 32-bit program, the size would most likely be 20 bytes because that's how big the array is. In a 64-bit program, because of padding, that could be bumped up to 24 bytes.

Barmar
  • 741,623
  • 53
  • 500
  • 612
zneak
  • 134,922
  • 42
  • 253
  • 328