ANSI C工会 - 他们真的有用吗? [英] ANSI C unions - are they really useful?

查看:104
本文介绍了ANSI C工会 - 他们真的有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从反应有些问题,昨天我才知道这是不可移植和不安全写入到一个工会成员和阅读不同类型的另一名成员的值,假设其成员的基本一致。所以经过一番研究,我发现,重复这一说法,并指定一个通俗的例子书面源 - 使用INT工会和float找到一个浮动的二进制重新presentation

From a response to some question yesterday, I learned that it is nonportable and unsafe to write into one union member and read the value from another member of a different type, assuming underlying alignment of the members. So after some research I found a written source that repeats this claim and specifies a popular example - using union of int and float to find the binary representation of a float.

所以,理解,这一假设并不安全,我不知道 - 除了节省内存(废话...)什么是真正的用途是有工会

So, understanding that this assumption is not safe, I wonder - except for saving memory (duh...) what real use is there to unions?

注:即,在标准C。显然,对于一个具体的实施方式中,规则是事先已知的,并且可以采取的优点

Note: that is, under Standard C. Clearly, for a specific implementation, the rules are known in advance and can be taken advantage of.

编辑:单词不安全,由于近年来的关联,可能是措辞的一个不错的选择,但我认为在明确意图

the word "unsafe", due to association of recent years, is probably a bad choice of wording, but I think the intention in clear.

编辑2:由于在回答这点重复 - 节省内存是一个有效的参数。我想知道,如果有一些超出了。

EDIT 2: Since this point repeats in the answers - saving memory is a valid argument. I wanted to know if there was something beyond that.

推荐答案

的提供创建通用集装箱的方法。不过,要获得多态行为,你必须实现一个虚函数表或类型切换自己......

The provide a way of creating generic containers. Though, to get polymorphic behavior you must implement a vtable or type switching yourself...

的,但是,这些功能之一,你只有当你需要他们,需要相当很少使用。

There are, however, one of those features that you only use when you need them and need rather rarely.

这篇关于ANSI C工会 - 他们真的有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆