Thread: Unions?

  1. #1
    Registered User
    Join Date
    May 2002


    Okay so i'm reading through my programming book and come back across the forgotten union. I'm sitting here reading it thinking, when, at least in console based c++ are unions actually useful? I can understand them if you have a huge program that accesses memory alot, but this doesn't seem feasible at least in the console. Can anyone provide a decent example of when this might actually be used?
    Oi Oi Oi!!!!

  2. #2
    Join Date
    Aug 2001
    Groningen (NL)
    Unions are very useful. Just an example: you can define a bitfield of 8 bits and a byte declared as unsigned char. Put those two in an union and then you can assign a received byte to the byte in the union and with the bitfield in the union you can access the bits individually.

    In some cases it is useful when you want to decrease the amount of used memory. If you have to use an int in the first part of the function and a char in the second part. Then you can declare two variables, one of type int and one of type char, but you could also introduce an union. Ofcourse this is only useful if the first variable isn't used anymore before using the second.

  3. #3
    Registered User
    Join Date
    May 2002
    A use of unions doesn't necessarily involve needing hteme only when a program gets big, actually its just smart use of memory through an entire program. A good example would be that for different screen modes a Pixel may be a different lenth of memory, fo example:
    union PixelType{
             unsigned char Eightbit;
             unsigned short Sixteenbit;
             unsigned long ThirtyTwobit;
    PixelType pixel;
    if(screenmode == 8)
               pixel.eightbit = color8;
    if(screenmode == 16)
               pixel.Sixteenbit = color16;
    if(screenmode == 32)
               pixel.ThiryTwobit = color32;
    However I rarely use them, only because of the ability templates gives you however.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    I wrote a program that creates some binary code at then creates a thread and runs the code I created.......

    In calculating jumps for the function calls I had to address 32 bit variables on a byte by byte basis......

    So when I had my 32bit address I stored it in a union like so;

    union MYUNION{
            UINT uiMemPos; //32 bit
            UCHAR ucMemPos; // 8 bit
    Then by switching the member to the 1 byte member I could assign a pointer to a UCHAR and I was away!

    Dont know if this was the best way to do this......but it worked pretty well........

    I dont define unions much at all (though I use them occasionally in preconfigured structures).......they are just a tool worth knowing about for certain situations

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions of the same size and pointers
    By holychicken in forum C Programming
    Replies: 9
    Last Post: 10-06-2008, 03:29 PM
  2. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  3. Structures - Unions
    By AProg in forum C Programming
    Replies: 16
    Last Post: 05-27-2003, 12:43 AM
  4. unions problems!
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2003, 04:04 AM
  5. Unions in c++
    By The Gweech in forum C++ Programming
    Replies: 5
    Last Post: 08-06-2002, 08:14 AM