为什么联合适用于 C 语言的低级系统编程?
在一本 C 语言教科书上,我读到联合体在低级系统编程中非常有用。我想知道为什么会这样?
为什么联合是低级系统编程的一个不错的选择?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,你的教科书应该解释它的意思——我们只能猜测其意图。
然而,我在操作系统驱动程序等中看到的一种用途是将设备的硬件寄存器映射到多个视图。因此,如果 IO 设备具有由字表示的 io 地址,您可能希望对整个字或高位字节等进行寻址。
虽然您可以使用指针并对其进行强制转换来做到这一点,但更清晰的方法是使用并集其中
现在允许通过
byte.high
等来寻址整个word
或其中的一部分。但是 union 并不是 struct 的替代品或替代品——它们有不同的用途——其中一个目的可能是拥有不同的同一记忆的视图。
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
which now allows addressing the entire
word
or part of it bybyte.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
允许您拥有不同的类型,但任何时候实际上只能使用其中一种类型。因此,如果您处于受限环境中,那么这可能很有用。另一方面,
struct
将为其中的所有类型保留内存,这意味着您正在使用更多内存,尽管您现在可以使用struct
中的所有变量那个一定比另一个更好,我不知道这是一个正确的说法。它们有两个不同的目的。
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 thestruct
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?
如前所述:
union
和struct
不具有相同的目的。如果您想要执行字节操作,则联合非常有用,例如:
您可以轻松访问变量
i
的某些字节,而无需进行困难的强制转换,这在低级编程中很常见。As already stated:
union
andstruct
don't serve the same purpose.Unions are useful if you want to do byte operations, for example:
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
对于遇到的每个
union
,您可能会遇到 10-100 个struct
。这两个(
struct
和union
)是相关的,但它们执行不同的工作。当您需要在不同时间将不同的值存储在单个空间单位中时,可以使用联合来节省空间。结构体更普遍有用;它收集了许多需要一起存储和访问的值(并且不能共享空间)。是的,
union
在低级编程中很有用。struct
也是如此。两者并不比另一个更好,因为他们从事不同的工作。当您需要struct
时,不能使用union
。您可以在不使用union
的情况下生存,但是当它们有用时,union
就非常有用。如果要在只能使用其中一个或另一个之间进行选择,我会选择每次都能使用
struct
。You are likely to encounter 10-100
struct
s for everyunion
that you encounter.The two (
struct
s andunion
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, canstruct
. Neither is better than the other because they do different jobs. You can't use aunion
when you need astruct
. You can survive without usingunion
, 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.