Find the answer to your Linux question:
Results 1 to 5 of 5
Hi , Can anybody will guide me , as what is the application of Union in real time ? . Thanks, -Amar...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Location
    Bangalore >> India
    Posts
    28

    Question What is the application of Union in realtime ?


    Hi ,

    Can anybody will guide me , as what is the application of Union in real time ? .


    Thanks,
    -Amar

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,558
    Could you please be more explicit as to what you are asking? Are you talking about union structures in a language like C, or something else? And what do you mean by real time? Are you talking about real time as in wall-clock time, or real time as in deterministic scheduling systems?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Location
    Bangalore >> India
    Posts
    28
    Yup,

    I am talking about union structures in a language like C.

    Now come to realtime, :- Real time in usual practical life. in which type project and where we can use it and HOW ?

    Thanks
    -Amar

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Gotcha. You are curious what people use unions for in real life.

    To start, I have yet to see an actual example of a union being used, so this is really theoretical. They're not particularly useful for most applications.

    One thing that you _can_ do with a union is crude subclassing in C. Imagine a setup like this:
    Code:
    struct square
    {
        int side;
    };
    
    struct circle
    {
        int radius;
    };
    
    struct rectangle
    {
        int side1;
        int side2;
    };
    
    union shape_union
    {
        struct rectangle *rect;
        struct square *squa;
        struct circle *circ;
    };
    
    enum shape_type = { RECTANGLE, SQUARE, CIRCLE };
    
    struct shape_struct
    {
        union shape_union shape_u;
        shape_type st;
    };
    
    typedef struct shape_struct shape;
    We can now write a single function that takes in a shape and computes its area:
    Code:
    int area(shape *s)
    {
        if(s == NULL)
            return -1;
    
        switch(s->st)
        {
            RECTANGLE:
                return st->rect->side1 * st->rect->side2;
            SQUARE:
                return st->squa->side * st->squa->side;
            CIRCLE:
                return st->circ->radius * 2 * PI;
            default:
                /* error */
                return -1;
        }
    }
    This works by using a union to store any type of shape, and wrapping it in a struct that keeps track of exactly what kind of shape we have.


    Does this make sense?

  5. #5
    Just Joined!
    Join Date
    Dec 2007
    Location
    Bangalore >> India
    Posts
    28
    Thanks yar .. goos explaination...

    i will try with new stuffs and i ll post ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •