Search code examples
memorydunions

Do unions occupy as much memory as the largest type of their member?


There is one thing confusing me about unions in the dlang, how much memory do the unions occupy? For example, I have the following union:

union Tqvar{
  double d;
  char c;
}

the double type takes 64 bits (if I'm not wrong), and char takes 8. Since unions can contain only one value (or whatever-you-call-it) at a time, if I store a char in it, in c, will the union occupy 8 bits or 64 bits?


Solution

  • Yes, unions in D, like in any language, must always have sufficient memory footprint to store their largest member. So it will take up 64bits, plus whatever overhead for the Union container itself, if any.