Link to home
Start Free TrialLog in
Avatar of VivekGupta22
VivekGupta22

asked on

Is there any padding possible within a unon

Is there any padding possible within a union like there iare padding fields within a structure.
Avatar of idt
idt

If you are asking if byte alignment still applies in a union, then the answer is yes
ASKER CERTIFIED SOLUTION
Avatar of idt
idt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi VivekGupta22,
> Is there any padding possible within a union

Yes, you can add a "dummy" array:

union my_union {
  int real_data_a;
  long double real_data_b;
  char dummy_padding[16];
};

This will make sure that the union is padded to 16 bytes, regardless of how long double is implemented on your machine (i.e., 80 bit on Intel, 128 on SPARC).


Cheers,
Stefan