Unions vs Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Unions vs Classes

    Some developers in my group are using UNIONS to define their data
    types in a C++ program for an embedded system. Are there any pro and
    cons in doing this when you can define a CLASS to do the same thing?

    I guess there might be some additional overhead with CLASSES, but is
    that really an issue with today's computers?
  • Karl Heinz Buchegger

    #2
    Re: Unions vs Classes

    David wrote:[color=blue]
    >
    > Some developers in my group are using UNIONS to define their data
    > types in a C++ program for an embedded system. Are there any pro and
    > cons in doing this when you can define a CLASS to do the same thing?
    >[/color]

    'union' and 'class' do different things.
    [color=blue]
    > I guess there might be some additional overhead with CLASSES, but is
    > that really an issue with today's computers?[/color]

    For todays desktop systems the memory savings by using a union
    is seldome an issue. But embedded system are notorious short
    on memory.

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • JKop

      #3
      Re: Unions vs Classes

      David posted:
      [color=blue]
      > Some developers in my group are using UNIONS to define their data
      > types in a C++ program for an embedded system. Are there any pro and
      > cons in doing this when you can define a CLASS to do the same thing?
      >
      > I guess there might be some additional overhead with CLASSES, but is
      > that really an issue with today's computers?[/color]

      Let's say that


      int = 32 bits

      char = 8 bits

      short = 16 bits


      Take the following:

      struct MonkeyStruct
      {
      int a;
      char b;
      short c;
      };

      struct MonkeyUnion
      {
      int a;
      char b;
      short c;
      };


      sizeof(MonkeySt ruct) will be 56 bits.

      sizeof(MonkeyUn ion) will be 32 bits.


      You can't simply just turn a class into a union or vice-versa! They're two
      separate types of entity. Why would a union be in any way superior to a
      struct? I myself can only think of two possible reasons:

      A) Conserve memory

      B) Neat memory tricks (like using a 4 char union to determine if a system is
      LITTLEENDIN or BIGENDIN)



      -JKop

      Comment

      • Siemel Naran

        #4
        Re: Unions vs Classes

        "David" <mail@david-eng.com> wrote in message
        [color=blue]
        > I guess there might be some additional overhead with CLASSES, but is
        > that really an issue with today's computers?[/color]

        Depends on the memory. With a union you could reduce the sizeof by 25% or
        50% or more. If you create many such classes, then the savings add up. On
        the other hand, if you need a type field, you might be better off with
        polymorphic classes (ie. virtual functions) as the virtual pointer is itself
        a type field.


        Comment

        • Xenos

          #5
          Re: Unions vs Classes


          "JKop" <NULL@NULL.NULL > wrote in message
          news:99Ezc.2285 $Z14.2284@news. indigo.ie...[color=blue]
          > David posted:
          >
          > Let's say that
          >
          >
          > int = 32 bits
          >
          > char = 8 bits
          >
          > short = 16 bits
          >
          >
          > Take the following:
          >
          > struct MonkeyStruct
          > {
          > int a;
          > char b;
          > short c;
          > };
          >
          > struct MonkeyUnion
          > {
          > int a;
          > char b;
          > short c;
          > };
          >
          >
          > sizeof(MonkeySt ruct) will be 56 bits.[/color]

          Though its implemetation specific, this will most likely be 64, because a 16
          short will probably require an alignment of mod 2.



          Comment

          • Rolf Magnus

            #6
            Re: Unions vs Classes

            Xenos wrote:
            [color=blue]
            > "JKop" <NULL@NULL.NULL > wrote in message
            > news:99Ezc.2285 $Z14.2284@news. indigo.ie...[color=green]
            >> David posted:
            >>
            >> Let's say that
            >>
            >>
            >> int = 32 bits
            >>
            >> char = 8 bits
            >>
            >> short = 16 bits
            >>
            >>
            >> Take the following:
            >>
            >> struct MonkeyStruct
            >> {
            >> int a;
            >> char b;
            >> short c;
            >> };
            >>
            >> struct MonkeyUnion
            >> {
            >> int a;
            >> char b;
            >> short c;
            >> };
            >>
            >>
            >> sizeof(MonkeySt ruct) will be 56 bits.[/color]
            >
            > Though its implemetation specific, this will most likely be 64,
            > because a 16 short will probably require an alignment of mod 2.[/color]

            Even without the char, the size would probably be 64 bits, because the
            the 4 byte int would need be aligned on a 4 byte boundary, and you have
            to remember that the struct must also be usable in an array.

            Comment

            • puppet_sock@hotmail.com

              #7
              Re: Unions vs Classes

              JKop <NULL@NULL.NULL > wrote in message news:<99Ezc.228 5$Z14.2284@news .indigo.ie>...
              [snip][color=blue]
              > Why would a union be in any way superior to a
              > struct? I myself can only think of two possible reasons:
              >
              > A) Conserve memory
              >
              > B) Neat memory tricks (like using a 4 char union to determine if a system is
              > LITTLEENDIN or BIGENDIN)[/color]

              Are you intending to do something like put an integer in and
              see what order the bytes come back out?

              I'm doubting this last would be safe. You should not count on
              how a compiler is going to squash things together in a union.
              Socks

              Comment

              • David Rubin

                #8
                Re: Unions vs Classes

                puppet_sock@hot mail.com wrote in message news:<c7976c46. 0406151357.2b6b 8569@posting.go ogle.com>...[color=blue]
                > JKop <NULL@NULL.NULL > wrote in message news:<99Ezc.228 5$Z14.2284@news .indigo.ie>...
                > [snip][color=green]
                > > Why would a union be in any way superior to a
                > > struct? I myself can only think of two possible reasons:
                > >
                > > A) Conserve memory
                > >
                > > B) Neat memory tricks (like using a 4 char union to determine if a system is
                > > LITTLEENDIN or BIGENDIN)[/color]
                >
                > Are you intending to do something like put an integer in and
                > see what order the bytes come back out?[/color]

                Given

                union Int2Bytes{
                int i;
                char b[sizeof(int)];
                };

                I know you cannot write to .i and read from .b, but is it legal to do this?

                int i = 0xaabbccdd;
                char b = ((Int2Bytes*)&i )->b[0];

                /david

                Comment

                • Jack Klein

                  #9
                  Re: Unions vs Classes

                  On 15 Jun 2004 06:19:17 -0700, mail@david-eng.com (David) wrote in
                  comp.lang.c++:
                  [color=blue]
                  > Some developers in my group are using UNIONS to define their data
                  > types in a C++ program for an embedded system. Are there any pro and
                  > cons in doing this when you can define a CLASS to do the same thing?
                  >
                  > I guess there might be some additional overhead with CLASSES, but is
                  > that really an issue with today's computers?[/color]

                  What do "today's computers" have to do with the embedded system?

                  It might make a vast difference if the target platform is an 8051 core
                  with 8K 8-bit bytes of code space and 128 (not K) 8-bit bytes of data
                  space on the one hand, and an ARM or PowerPC with many megabytes of
                  both.

                  Embedded systems vary so widely and by so many orders of magnitude
                  that the difference between common desk top computers and their
                  operating systems _almost_ seems trivial by comparison.

                  What do the developers who prefer unions state is the reason for
                  selecting them over classes?

                  --
                  Jack Klein
                  Home: http://JK-Technology.Com
                  FAQs for
                  comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                  comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                  alt.comp.lang.l earn.c-c++

                  Comment

                  • Siemel Naran

                    #10
                    Re: Unions vs Classes

                    "Jack Klein" <jackklein@spam cop.net> wrote in message
                    [color=blue]
                    > What do the developers who prefer unions state is the reason for
                    > selecting them over classes?[/color]

                    I've heard it's used extensively in low level programming. Also, if you
                    want to write an API and works in both C++ and C, then unions might be
                    right, as in the sockets class.


                    Comment

                    • Chris Hill

                      #11
                      Re: Unions vs Classes

                      On Wed, 16 Jun 2004 04:09:02 GMT, "Siemel Naran"
                      <SiemelNaran@RE MOVE.att.net> wrote:
                      [color=blue]
                      >"Jack Klein" <jackklein@spam cop.net> wrote in message
                      >[color=green]
                      >> What do the developers who prefer unions state is the reason for
                      >> selecting them over classes?[/color]
                      >
                      >I've heard it's used extensively in low level programming. Also, if you
                      >want to write an API and works in both C++ and C, then unions might be
                      >right, as in the sockets class.
                      >[/color]

                      I have seen the situation of auto generate interface code that used
                      structs to hold different data and then collected these together
                      using unions and a type field. The client would inspect the type
                      field and then delve into the correct bit of the struct/union
                      combination.

                      This was actually consuming more space than a class hierarchy
                      and proper polymorphism. Since, the unions were as large as
                      the largest member. I suspect that paying the price of VPTRs
                      and using a class hierarchy (even with multiple inheritance) would
                      consume less space.

                      :) Chris.

                      Comment

                      • JKop

                        #12
                        Re: Unions vs Classes

                        posted:
                        [color=blue]
                        > JKop <NULL@NULL.NULL > wrote in message
                        > news:<99Ezc.228 5$Z14.2284@news .indigo.ie>... [snip][color=green]
                        >> Why would a union be in any way superior to a
                        >> struct? I myself can only think of two possible reasons:
                        >>
                        >> A) Conserve memory
                        >>
                        >> B) Neat memory tricks (like using a 4 char union to determine if a
                        >> system is LITTLEENDIN or BIGENDIN)[/color]
                        >
                        > Are you intending to do something like put an integer in and
                        > see what order the bytes come back out?
                        >
                        > I'm doubting this last would be safe. You should not count on
                        > how a compiler is going to squash things together in a union.
                        > Socks[/color]

                        union {

                        unsigned int FourBytes;

                        struct {
                        unsigned char Byte1;
                        unsigned char Byte2;
                        unsigned char Byte3;
                        unsigned char Byte4;
                        };
                        } blind;



                        int main(void)
                        {
                        blind.FourBytes = 0x1F1F1F1F;

                        //Now, check each individual byte

                        if (Byte1 ==


                        }



                        I did this before in a program I was writing. Can't remember why I did it,
                        or which numbers I used! I'd still have it tucked away in my archive though
                        (Actually, just right now as I'm writing this I thought of the reason, I was
                        encoding digital audio to be sent down to a CD-Writer, and had to know the
                        ENDINESS).


                        -JKop

                        Comment

                        • Siemel Naran

                          #13
                          Re: Unions vs Classes

                          "Chris Hill" <news1@plantain .org.uk> wrote in message
                          [color=blue]
                          > This was actually consuming more space than a class hierarchy
                          > and proper polymorphism. Since, the unions were as large as
                          > the largest member. I suspect that paying the price of VPTRs
                          > and using a class hierarchy (even with multiple inheritance) would
                          > consume less space.[/color]

                          Furthermore the VPTR way is probably faster than a switch-case on the type
                          field.


                          Comment

                          • Bill Seurer

                            #14
                            Re: Unions vs Classes

                            JKop wrote:
                            [color=blue]
                            > union {
                            > unsigned int FourBytes;
                            >
                            > struct {
                            > unsigned char Byte1;
                            > unsigned char Byte2;
                            > unsigned char Byte3;
                            > unsigned char Byte4;
                            > };
                            > } blind;
                            >
                            > int main(void)
                            > {
                            > blind.FourBytes = 0x1F1F1F1F;
                            > //Now, check each individual byte
                            > if (Byte1 ==
                            >
                            > I did this before in a program I was writing. ...[/color]

                            While using unions is generally machine specific anyway the above is
                            especially "bad" because it makes many assumptions (like ints are 4 bytes).

                            Comment

                            • puppet_sock@hotmail.com

                              #15
                              Re: Unions vs Classes

                              davidrubin@warp mail.net (David Rubin) wrote in message news:<82b37be.0 406151809.13116 930@posting.goo gle.com>...
                              [puppet sock moaning about in-one-var-out-the-other unions snipped][color=blue]
                              > Given
                              >
                              > union Int2Bytes{
                              > int i;
                              > char b[sizeof(int)];
                              > };
                              >
                              > I know you cannot write to .i and read from .b,[/color]

                              You can, it's just that it is not portable, and probably even
                              not reliable. Some things that could bust it:
                              - Different hardware
                              - Different OS (even new version)
                              - Different version of the compiler
                              - Different address of loading the code into memory
                              - Long list of weird things that could happen on specialist platforms
                              [color=blue]
                              > but is it legal to do this?
                              >
                              > int i = 0xaabbccdd;
                              > char b = ((Int2Bytes*)&i )->b[0];
                              >[/color]

                              I'm pretty confused by that. What's it supposed to do?
                              Lessee, you've got the address of an int variable, and
                              you've cast that as a pointer to an undeclared union,
                              and you've tried to reference the zeroeth char in the
                              "b" data element of that undeclared union. I'd say,
                              that this *might* compile, but the results won't be good.
                              The best you could hope for would be some kind of memory
                              access violation. The worst would be that char b contains
                              garbage and you don't realize it.
                              Socks

                              Comment

                              Working...