This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how many mem does union cost?

struct structSeperate
{
unsigned char uaHigh;
unsigned char uaLow;
};
union OneIntorTwoBytes
{
unsigned int un;
struct structSeperate ua;
};

void AFunction()
{
OneIntorTwoBytes unionMy;
...
...unionMy...//
...
}

In the above example, Could you please explain how many bytes of free mem will be allocated for AFunction()? How many bytes of free mem will be allocated for the whole module?

Parents
  • The only time a Union (or struct) will "cost" you extra memory beyond the size of its contents is if the compiler inserts any extra "padding" bytes for alignment.
    AFAIK, Keil C51, being targetted on an 8-bit architecture, doesn't add padding.

    Padding is usually only added on larger word-size machines, which don't like objects crossing word boundaries; eg,
    16-bit objects starting on odd addresses for 16- or 32-bit machines;
    32-bit objects starting on non-multiple-of-4 addresses on 32-bit machines.

    There is usually some sort of "pack" option to overrinde the padding, at the cost of access efficiency.

Reply
  • The only time a Union (or struct) will "cost" you extra memory beyond the size of its contents is if the compiler inserts any extra "padding" bytes for alignment.
    AFAIK, Keil C51, being targetted on an 8-bit architecture, doesn't add padding.

    Padding is usually only added on larger word-size machines, which don't like objects crossing word boundaries; eg,
    16-bit objects starting on odd addresses for 16- or 32-bit machines;
    32-bit objects starting on non-multiple-of-4 addresses on 32-bit machines.

    There is usually some sort of "pack" option to overrinde the padding, at the cost of access efficiency.

Children
No data