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
  • Don't forget those &ltpre&gt and &lt/pre&gt tags!

    This is a popular way to access individual bytes of a larger data type, but don't forget that it's non-portable - as it relies entirely upon the byte ordering of the Compiler/Target.
    There may also be alignment issues with some larger data types.

    You might want to try something like:

    struct structSeperate
    {
    #if defined __C51__ 
       unsigned char uaHigh;
       unsigned char uaLow;
    #elif defined SOME_OTHER_COMPILER
       unsigned char uaLow;
       unsigned char uaHigh;
    #else
    #error Unknown byte ordering
    #endif
    };
    union OneIntorTwoBytes
    {
       unsigned int          un;
       struct structSeperate ua;
    };

    On the subject of unions, alignment and Keil Gotchas!, you should also check these Knowledge base articles:
    http://www.keil.com/support/docs/928.htm
    http://www.keil.com/support/docs/1279.htm

Reply
  • Don't forget those &ltpre&gt and &lt/pre&gt tags!

    This is a popular way to access individual bytes of a larger data type, but don't forget that it's non-portable - as it relies entirely upon the byte ordering of the Compiler/Target.
    There may also be alignment issues with some larger data types.

    You might want to try something like:

    struct structSeperate
    {
    #if defined __C51__ 
       unsigned char uaHigh;
       unsigned char uaLow;
    #elif defined SOME_OTHER_COMPILER
       unsigned char uaLow;
       unsigned char uaHigh;
    #else
    #error Unknown byte ordering
    #endif
    };
    union OneIntorTwoBytes
    {
       unsigned int          un;
       struct structSeperate ua;
    };

    On the subject of unions, alignment and Keil Gotchas!, you should also check these Knowledge base articles:
    http://www.keil.com/support/docs/928.htm
    http://www.keil.com/support/docs/1279.htm

Children
No data