# Thread: Structure and union size allocation

1. ## Structure and union size allocation

I am not sure about the sizes printed to this output ..............
any reply is appreciated
Thanks
Code:
```/*E11_9*/
#include<stdio.h>

struct {
char a[20];
int b;

union {
double c;

struct {
char d[15];
float e;
} x;
} y;
} z;

int main(void) {
printf("%u\n",sizeof (z));
return 0;
}```
outupu:20 24 28

2. There is only one printf call, and it only prints one value, so I am not sure how you get three values printed.

Anyway, you need to tell us what problems you have interpreting or understanding the code and/or output.

3. sry printf("%u%u%u\n",sizeof(z),sizeof(y),sizeof(x));

4. so the above is the modification

5. Alright, this is the only way I could get your code to compile silently under
Code:
`gcc -std=c11 -Wall -Wextra -pedantic -O3`
Code:
```#include <stdio.h>

struct z
{
char a[20];
int  b;

union
{
double c;

struct
{
char d[15];
float e;
} x;
} y;
};

int main(void)
{
struct z test;

printf("sizeof(z) == %zd\n", sizeof(test));
printf("sizeof(y) == %zd\n", sizeof(test.y));
printf("sizeof(x) == %zd\n", sizeof(test.y.x));

return 0;
}```
Output :
Code:
```sizeof(z) == 48
sizeof(y) == 24
sizeof(x) == 20```
Basically, the compiler is doing automatic alignment for these types.

For x, 15 + 4 = 19 which would align to 20.

As for why y is 24, I'd only image because you have 20 and 4 so for the compiler to properly align that, it uses 24 bytes.

And as for z, it takes that previous 24 and then adds the other 20 + 4 bytes so you wind up with 48.

Read up on memory alignment and stuff like that.

compiler construction - Why do we have alignment padding if memory is byte-addressable? - Stack Overflow

6. Actually, here's some updated code that shows the offsets from the base of the structure.
Code:
```#include <stdio.h>
#include <stddef.h>

struct z
{
char a[20];
int  b;

union
{
double c;

struct
{
char d[15];
float e;
} x;
} y;
};

int main(void)
{
struct z test;

printf("sizeof(z) == %zd\n", sizeof(test));
printf("sizeof(y) == %zd\n", sizeof(test.y));
printf("sizeof(x) == %zd\n", sizeof(test.y.x));

ptrdiff_t base = (ptrdiff_t ) &test;

ptrdiff_t a = (ptrdiff_t ) &(test.a);
ptrdiff_t b = (ptrdiff_t ) &(test.b);

ptrdiff_t c = (ptrdiff_t ) &(test.y.c);

ptrdiff_t d = (ptrdiff_t ) &(test.y.x.d);
ptrdiff_t e = (ptrdiff_t ) &(test.y.x.e);

printf("a - base == %td\n", a - base);
printf("b - base == %td\n", b - base);
printf("c - base == %td\n", c - base);
printf("d - base == %td\n", d - base);
printf("e - base == %td\n", e - base);

return 0;
}```
Output :
Code:
```sizeof(z) == 48
sizeof(y) == 24
sizeof(x) == 20
a - base == 0  // a starts at start of structure
b - base == 20 // b begins 20 bytes after
c - base == 24 // c starts where it should
d - base == 24 // d also starts where it should
e - base == 40 // then, 16 bytes later, we have e (15 would be aligned to 16)```
As for why the total is 48 and not 44, I'm not sure considering most floats are 4 bytes. Again, probably weird compiler alignment but this sort of shows what's going on.

7. Originally Posted by MutantJohn
As for why the total is 48 and not 44, I'm not sure considering most floats are 4 bytes. Again, probably weird compiler alignment but this sort of shows what's going on.
Since sizeof(y) is 24, the only way the total could be 44 would be if an int (member named b) had size zero. Which is not possible.

Popular pages Recent additions