为什么联合适用于 C 语言的低级系统编程?

发布于 2024-12-03 16:56:37 字数 80 浏览 3 评论 0原文

在一本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

绳情 2024-12-10 16:56:37

好吧,你的教科书应该解释它的意思——我们只能猜测其意图。

然而,我在操作系统驱动程序等中看到的一种用途是将设备的硬件寄存器映射到多个视图。因此,如果 IO 设备具有由字表示的 io 地址,您可能希望对整个字或高位字节等进行寻址。

虽然您可以使用指针并对其进行强制转换来做到这一点,但更清晰的方法是使用并集其中

  union {
      unsigned short word;
      struct {
           unsigned char high;
           unsigned char low;
      } byte;
   };

现在允许通过 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

  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.

谎言 2024-12-10 16:56:37

union允许您拥有不同的类型,但任何时候实际上只能使用其中一种类型。因此,如果您处于受限环境中,那么这可能很有用。

另一方面,struct 将为其中的所有类型保留内存,这意味着您正在使用更多内存,尽管您现在可以使用 struct 中的所有变量

那个一定比另一个更好,我不知道这是一个正确的说法。它们有两个不同的目的。

unions 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.

吃不饱 2024-12-10 16:56:37

联合和结构是两种不同的构造。说一个比另一个更好是无稽之谈。

菠萝比哲学更适合游泳吗?

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?

好久不见√ 2024-12-10 16:56:37

如前所述:unionstruct 不具有相同的目的。

如果您想要执行字节操作,则联合非常有用,例如:

union {
   int i;
   char pi[4];
}

您可以轻松访问变量 i 的某些字节,而无需进行困难的强制转换,这在低级编程中很常见。

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.

国际总奸 2024-12-10 16:56:37

工会与两者的结构在不同情况下用于不同目的。!你永远不能使用联合来代替结构和;反之亦然
为了更好地理解,要清楚结构和内容工会规格

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

谈场末日恋爱 2024-12-10 16:56:37

对于遇到的每个 union,您可能会遇到 10-100 个 struct

这两个(structunion)是相关的,但它们执行不同的工作。当您需要在不同时间将不同的值存储在单个空间单位中时,可以使用联合来节省空间。结构体更普遍有用;它收集了许多需要一起存储和访问的值(并且不能共享空间)。

是的,union 在低级编程中很有用。 struct 也是如此。两者并不比另一个更好,因为他们从事不同的工作。当您需要struct时,不能使用union。您可以在不使用 union 的情况下生存,但是当它们有用时,union 就非常有用。

如果要在只能使用其中一个或另一个之间进行选择,我会选择每次都能使用struct

You are likely to encounter 10-100 structs for every union that you encounter.

The two (structs and unions) 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, unions 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.

他不在意 2024-12-10 16:56:37

这是错误的——你不能改变工会的结构。区别在于内存使用情况。在结构中,所有变量在内存中都占据不同的位置。所以结构的大小是它的变量的总和(通常+填充)。

另一方面,联合体与其最大变量一样大,所有变量都驻留在内存中的一个位置,因此修改一个变量会更改联合体中其他变量的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文