Why are unions good for low level system programming in C? [closed]
In one of the textbooks on C, I have read that unions are very useful when it comes to low level system programming. I would like to know why is it so?
Why would unions be a good choice for low level system programming?
Well, your text book should explain what it means -- we can only second guess the intention.
However one use i have seen in os-drivers and the like is the mapping of hardware registers for the device to multiple views. So if a IO device have a io-address represented by a word, you may want to address the entire word or the high order byte etc.
While you can do that with pointers and casting them, then a clearner way is to have a union of
union {
unsigned short word;
struct {
unsigned char high;
unsigned char low;
} byte;
};
which now allows addressing the entire word
or part of it by byte.high
etc.
However union is not a replacement or alternative for struct -- they serve different purposes -- and one such purpose could be to have different view of the same memory.
union
s allow you to have different types but only one of them can actually be used at any time. So if you are in a constrained environment for example, then this could be useful.
A struct
on the other hand, will reserve memory for all it's types inside it, which means you are using more memory, though you can now use all the variables in the struct
That one is necessarily better then the other, I don't know that this is a correct statement. They serve two different purposes.
Unions and structures are two different constructs. It is nonsense to say that one is better than the other.
Are pineapples better than philosophy for swimming?
As already stated: union
and struct
don't serve the same purpose.
Unions are useful if you want to do byte operations, for example:
union {
int i;
char pi[4];
}
You can easily access some bytes of the variable i
without doing difficult casts, which is very common in low level programming.
Unions & structure both are used in different situation for different purpose.! you can never use union in place of structure & vice versa for better understaning be clear with structure & union's spec
You are likely to encounter 10-100 struct
s for every union
that you encounter.
The two (struct
s and union
s) are related, but they do different jobs. A union may be used to save space when you need to store different values in a single unit of space at different times. A structure is more generally useful; it collects together a number of values that need to be stored and accessed together (and which cannot share space).
Yes, union
can be useful in low-level programming. So, too, can struct
. Neither is better than the other because they do different jobs. You can't use a union
when you need a struct
. You can survive without using union
, but when they're useful, union
s are very useful.
Given a choice between being able to use only one or the other, I'd choose to be able to use struct
every time.
This is false - you can't change structure for unions. The difference is in memory usage. In structure all variables take separated places in memory. So the size of structure is sum of it's variables (often + padding).
On the other side unions are as big as their biggest variable, all variables are residing in 1 place in memory, so modifying one variable changes contents of other variables in union.
精彩评论