Thread: realtime example for union

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    realtime example for union

    Hi ,

    in so many interview i facing one question , like " can u give one realtime example for union use instead of structer". we working onlly on structure concepts.

    i know basic functionalites of unions and structures. i know differences also b/w them.

    can any body give example for the use of unions in realtime senario

  2. #2
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17
    One common real life application of unions is, to use generate device handles or similar entities. Given below is an example, where an 8-bit demux handle is constructed bitwise, and once it is constructed it is passed on to the demux APIs using the single 8-bit handler value.
    Code:
    typedef union Handle_s {
        unsigned char uiHandle;
        struct {
            unsigned char source:3;
            unsigned char sink:3;
            unsigned char mode:2;
        } HandleBits;
    } Handle_t;
    
    Handle_t DemuxHandle;
    DemuxHandle.HandleBits.source = 0x01;
    DemuxHandle.HandleBits.sink = 0x11;
    DemuxHandle.HandleBits.mode = 0x10;
    
    DemuxOpen (DemuxHandle.uiHandle);

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    "realtime" really makes little sense here but if you are looking for a "real-world" use I use it like the previous poster in that it makes bit-packing mucho easier. I recently had to work on a 128-bit system that used 128bit values as messages. Using a union I was able to pack the message sender, recv., msg ID (type) and the handle to a shared memory window into a single 128 bit integer. Another use that was in a similar vein was the use of unions to "cast" a 16-byte memory block into 4 integers, two doubles, etc. This was used in an event logging system (look in the Linux forum under multithreaded event logging) that used no text....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    A word of caution here is that if you initialize uiHandle later on, this will overwrite what you have initialized your struct members to. From my understanding here is that now uiHandle and your struct share the same memory footprint of the largest data declaration.

    ????

    Can someone clarify if you can still do this(below), even though defined within the union?

    Code:
    DemuxHandle.HandleBits = {0x1, 0x11, 0x10};
    Last edited by slingerland3g; 12-21-2009 at 04:37 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you try it and find out? It's not that hard to throw a few lines of code together to see.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by quzah View Post
    Did you try it and find out? It's not that hard to throw a few lines of code together to see.


    Quzah.

    Sorry, no I did not, looked at the clock and I had to catch my bus! Guess I should test before I post huh?

  7. #7
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17

    Correction

    Sorry, there is a small correction in my sample code. It struck to me just now. I gave that example without trying out (still did not try out!!), but the below assignment was what I had in mind when I posted first.
    Code:
    DemuxHandle.HandleBits.source = 1; /* 001(binary) */
    DemuxHandle.HandleBits.sink = 3;   /* 011(binary), NOT 0x11(17 or 0001 0001) */
    DemuxHandle.HandleBits.mode = 2;   /*  10(binary), NOT 0x10(16 or 0001 0000) */
    In my earlier example of 0x11 and 0x10, the values would've got clipped off, and would've been actually 1 instead of 17 (due to 3-bit limit) and 0 instead of 16 (due to 2-bit limit).
    Last edited by technam; 12-23-2009 at 01:14 AM. Reason: binary expansion (for better understanding)

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    i cant analysing the above code.i cant understanding any thing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-05-2009, 12:39 PM
  2. Replies: 5
    Last Post: 05-15-2009, 02:36 AM
  3. Input of realtime data
    By Marb in forum C Programming
    Replies: 1
    Last Post: 11-20-2008, 03:19 AM
  4. Direct3D displaying a pointlist from realtime co-ords
    By hoAx in forum Game Programming
    Replies: 0
    Last Post: 03-09-2008, 01:56 PM
  5. Realtime Timer
    By FromHolland in forum C++ Programming
    Replies: 3
    Last Post: 09-08-2003, 05:52 PM