Results 1 to 5 of 5
Hi ,
Can anybody will guide me , as what is the application of Union in real time ? .
Thanks,
-Amar...
- 09-01-2009 #1Just Joined!
- Join Date
- Dec 2007
- Location
- Bangalore >> India
- Posts
- 28
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
- 09-01-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
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!
- 09-03-2009 #3Just 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
- 09-03-2009 #4
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:
We can now write a single function that takes in a shape and computes its area: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;
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.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; } }
Does this make sense?DISTRO=Arch
Registered Linux User #388732
- 09-03-2009 #5Just Joined!
- Join Date
- Dec 2007
- Location
- Bangalore >> India
- Posts
- 28
Thanks yar .. goos explaination...
i will try with new stuffs and i ll post ...


Reply With Quote