工会和位域特别使用在哪里? [英] where are unions and bitfields particularly used ?

查看:63
本文介绍了工会和位域特别使用在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我几年的经验中,我从来没有找到一个

实例,我需要使用工会和位域(虽然我已经使用过

结构)。我只想象这些会发现

的相关性。尽管这些(位域和工会)都用在了

空间的地方一个约束(所以我总是可以假设在嵌入式系统中,其中内存特别少于内存)我们希望节省空间/内存。


至于我已经读过,除了节省空间之外,结构之间没有区别(这也意味着成员在工会中与结构相比是如何获得b $ b)。 br />

有人可以给出几个例子/场景'这些实际上已经使用或需要使用的b $ b。


我认为结构和工会是用户定义的

数据类型,直到我从K& R" Uni来看这个工会的定义

是一个VARIABLE,可以保存(在不同的时间)不同类型和大小的对象,而编译器跟踪大小和

对齐要求。


因此,联合是一个用户定义的数据类型或变量。


为什么会这样? ;联盟只能初始化为其第一个成员的类型

。来自K& R ??


TIA

rohit

解决方案

" ;罗希特夏尔" < PR ****** @ yahoo.com>在消息中写道

news:93 ************************* @ posting.google.co m ... < blockquote class =post_quotes>在我多年的经验中,我从来没有找到一个单一的
实例,我需要使用联合和位域(虽然我已经使用了
结构)。
[snip]有人可以给出几个实例/场景的实际使用或需要使用的示例/场景。


唯一一次我记得我广泛使用了一个联盟是我有一个

函数,它将返回多种数据格式中的一种。具体来说,

这是一个小型DNS客户端库的一部分;执行查询后,可以重复调用这个

函数来迭代

响应中的记录,每个响应可能是一个IP地址,一个域名,或者

别的东西。唯一的直接优势(如你所说)是节省空间。


位域通常用于非便携式代码(它们代表一些特定的布局位)。如果你有多个

标志,你可以使用它们 - 但我通常#define位值并使用单个flags

成员。


[snip]因此,联合是用户定义的数据类型或变量。


这两个都是。我读过变量。作为一个宽松的术语,意思是能够持有不同价值的b $ b的东西。

以及为什么联盟只能用值<初始化 br />其第一个成员的类型来自K& R ??




因为语言的语法/语法中没有规定

指定哪个成员为初始化 - 虽然我认为不再是C99的

案例。


Alex


"阿莱克斯" < me@privacy.net>写道:

" rohit" < PR ****** @ yahoo.com>在消息中写道
新闻:93 ************************* @ posting.google.co m ...

因此,联合是一个用户定义的数据类型或变量。



它们都是。我读过变量。作为一个宽松的术语,意思是能够持有不同价值的东西。




这个含糊不清经常出现在一个应该真正使用的单词

作为形容词用作名词。形容词联合这个问题没有

,即联合类型。和联合对象都是毫不含糊的。


马丁

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \ _ /


rohit写道:


在我几年的经验中,我从来没有找到过单一的
我需要使用工会和位域的实例(虽然我已经使用了
结构)。我只是想象这些会发现相关性。尽管这些(位域和工会)都被使用了
空间是一个约束(所以我总是假设嵌入式系统,内存特别少),我们想节省空间/内存。


位字段(在结构中)用于访问硬件或提供状态

有关结构的信息。

目前为止正如我所读到的那样,除了节省空间之外,结构之间没有区别(这也意味着成员在结合中与结构相比如何被访问)。


工会和结构之间有一个很大的区别:工会将为b
为列表中最大的项目分配空间。此外,编译器允许在字段之间添加空间。

有人可以给出一些示例/场景,其中这些已经实际使用过了吗?或需要使用。


Borlands的IDE使用工会来处理数据库字段。有一个变量

来定义字段类型和字段类别的联合。


我的结果是结构和联合是用户定义的
数据类型,直到我从K& R来看这个工会的定义,Union
是一个VARIABLE,它可以保存(在不同的时间)不同类型和大小的对象,而编译器跟踪大小和对齐要求。

因此,联合是用户定义的数据类型或变量。


工会是一个模板,显示数据的组织方式。这个模板是

用于实例化变量或分配内存。

为什么它是一个联合只能初始化值为
的类型其第一个成员来自K& R ??


可能简化编译器编写。想一想,

编译器如何知道联合中哪个数据类型初始化数据

代表?

TIA
rohit




关于位域的一个词:排序,最重要的位或

最低有效位,取决于编译器。许多在嵌入式系统上工作或者使用单个位工作的人,通常会在无符号整数变量上使用位b / b
算术运算符而不是

处理结构中的位域。

-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http:// www。 parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn .c-c ++ faq:
http:// www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍


Hi,
In my couple of years of experience, I have never found a single
instance where I needed to use unions and bitfields(though I have used
structures).I was just imagining where would these find
relevance.Though both of these(bitfields and unions) are used where
space is a constraint(so I can assume always in embedded systems,where
memory is particularly less)and we want to save space/memory.

As far as I have read, there is no difference between structs an
unions apart for saving space(that would also imply how members are
accessed in unions compared to in structs).

Can someone just give a couple of examples/scenario''s where these have
actually been used or need to be used.

I was of the impression that structures and unions were user defined
data types until I came to this defination of unions from K&R "Union
is a VARIABLE that may hold (at different times) objects of different
types and sizes, while the compiler keeping track of size and
alignment requirements."

So, is an union an user defined data type or a variable.

and why is it that "A union may only be initialized with a value of
the type of its first member" from K&R ??

TIA
rohit

解决方案

"rohit" <pr******@yahoo.com> wrote in message
news:93*************************@posting.google.co m...

In my couple of years of experience, I have never found a single
instance where I needed to use unions and bitfields(though I have used
structures). [snip] Can someone just give a couple of examples/scenario''s where these have
actually been used or need to be used.
The only time I can recall that I extensively used a union is where I had a
function which would return one of a number of data formats. Specifically,
this was part of a small DNS client library; having performed a query, this
function could be called repeatedly to iterate over the records in the
response, each one of which might be an IP address, a domain name, or
something else. The only direct advantage (as you noted) is saving space.

Bitfields are often used in non-portable code (where they represent some
particular layout of bits). You could use them if you have a number of
flags - but I generally #define the bit values and use a single "flags"
member.

[snip] So, is an union an user defined data type or a variable.
It''s both. I read "variable" as a loose term meaning "something capable of
holding different values."
and why is it that "A union may only be initialized with a value of
the type of its first member" from K&R ??



Because there''s no provision in the syntax/grammar of the language to
specify which member to initialise - although I think that is no longer the
case with C99.

Alex


"Alex" <me@privacy.net> writes:

"rohit" <pr******@yahoo.com> wrote in message
news:93*************************@posting.google.co m...

So, is an union an user defined data type or a variable.



It''s both. I read "variable" as a loose term meaning "something
capable of holding different values."



This ambiguity often arises when a word that should really only be used
as an adjective is used as a noun. The adjective "union" doesn''t have
this problem, i.e. "union type" and "union object" are both unambiguous.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/


rohit wrote:

Hi,
In my couple of years of experience, I have never found a single
instance where I needed to use unions and bitfields(though I have used
structures).I was just imagining where would these find
relevance.Though both of these(bitfields and unions) are used where
space is a constraint(so I can assume always in embedded systems,where
memory is particularly less)and we want to save space/memory.
Bit fields (in a struct) are used to access hardware or provide state
information about the structure.

As far as I have read, there is no difference between structs an
unions apart for saving space(that would also imply how members are
accessed in unions compared to in structs).
There is one big difference between unions and structs: a union will
allocate space for the largest item in the list. Also, compilers are
allowed to add space between fields.

Can someone just give a couple of examples/scenario''s where these have
actually been used or need to be used.
Borlands'' IDE uses unions for database fields. The have one variable
to define the kind of field and a union for the field categories.

I was of the impression that structures and unions were user defined
data types until I came to this defination of unions from K&R "Union
is a VARIABLE that may hold (at different times) objects of different
types and sizes, while the compiler keeping track of size and
alignment requirements."

So, is an union an user defined data type or a variable.
A union is a stencil showing how data is organized. This stencil is
used for instantiating variables or allocating memory.

and why is it that "A union may only be initialized with a value of
the type of its first member" from K&R ??
Probably to simplify compiler writing. Think about it, how does the
compiler know which data type in the union that the initialized data
is to represent?

TIA
rohit



One word about bitfields: The ordering, most significant bit or
least significant bit, is compiler dependent. Many people who work
on embbed systems or with individual bits, will often use the bit
arithmetic operators on an unsigned integral variable rather than
dealing with bitfields in a structure.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


这篇关于工会和位域特别使用在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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