本文介绍了与压缩结构联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

以下是示例代码段:

....

typedef PACKED struct

{

union

{

PACKED struct

{

char red :1;

char green:1;

char蓝色:1;

} color_bit;

char颜色;

} color_u;

} color_t;


color_t颜色;

....

我的问题是:color.color_u.color中的位是什么?反映

" color.color_u.color_bit.red"," color.color_u.color_bit.green"和

" color.color_u.color_bit.blue"?这个定义得好吗?

换句话说,如果我设置color.color_u.color_bit.red为0,

" color.color_u.color_bit.green" as 0和color.color_u.color_bit.blue

为1,什么是color.color_u.color?它是0x04吗?

解决方案




关于位字段的几乎所有内容都是实现定义的。它完全取决于你的编译器如何将位置放在它用于存储的类型中./ b
。检查编译器的文档。


-

Jack Klein

主页:



comp的常见问题解答.lang.c

comp.lang.c ++

alt.comp.lang.learn.c-c ++





不一定。事实上,并不能保证

`color.color_u.color''将具有有效值:工会

经常(ab)用于这种类型的惩罚,但

语言并不保证任何合理的将会发生如果你尝试的话会发生b $ b。


如果你想在一个

`char''中将颜色表示为三位,你最好使用按位运算符>
pre- #define(或者enum-erated)面具:


typedef unsigned char color_t;

#define COLOR_RED 1

#define COLOR_GREEN 2

#define COLOR_BLUE 4

#define COLOR_YELLOW(COLOR_GREEN | COLOR_RED)

...


- -

Eric Sosman
盖子




什么是包装?它不是C构造 - 看起来好像它可能是一个#defined宏扩展到(一个
会猜测)某种特定于实现的扩展对C来说比如`__attribute __((packed))''。如果是这样,你需要查阅你的实施文件,找出发生了什么,因为它不是C.

好吧,它*可能是*,如果'PACKED''扩展为像'volatile'或'const''或者什么都没有的东西。在接下来的内容中,我会假设情况就是这样。



不是用C语言编写的。编译器如何选择在结构中排列位字段有很大的自由。
不同的编译器会以不同的方式排列。



不一定。事实上,并不能保证
`color.color_u.color''具有有效值:联合
经常(ab)用于这种类型的惩罚,但
如果你想在一个字符中用三个字符表示颜色'',你最好使用带有
预定义(或枚举)掩码的按位运算符:

typedef unsigned char color_t;
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_BLUE 4
#define COLOR_YELLOW(COLOR_GREEN | COLOR_RED)
...

-
Eric Sosman
lid


Hi all,
Here is a sample code segment:
....
typedef PACKED struct
{
union
{
PACKED struct
{
char red:1;
char green:1;
char blue:1;
} color_bit;
char color;
} color_u;
} color_t;

color_t color;
....
My question is: which bit in "color.color_u.color" reflect
"color.color_u.color_bit.red", "color.color_u.color_bit.green" and
"color.color_u.color_bit.blue"? Is this well defined?
In other words, if I set "color.color_u.color_bit.red" as 0,
"color.color_u.color_bit.green" as 0 and "color.color_u.color_bit.blue"
as 1, What will be "color.color_u.color"? is it 0x04?

解决方案



Almost everything about bit-fields is implementation defined. It is
entirely up to your compiler as to how it places the bits in the type
it uses for storage. Check your compiler''s documentation.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html




Not necessarily. In fact, it''s not guaranteed that
`color.color_u.color'' will have a valid value at all: unions
are frequently (ab)used for this sort of type punning, but
the language doesn''t promise that anything reasonable will
happen if you try it.

If you want to represent a color as three bits in a
`char'', you''re better off using bitwise operators with
pre-#defined (or enum-erated) masks:

typedef unsigned char color_t;
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_BLUE 4
#define COLOR_YELLOW (COLOR_GREEN | COLOR_RED)
...

--
Eric Sosman
es*****@acm-dot-org.invalid




What is `PACKED''? It isn''t a C construct -- it looks
like it''s probably a #defined macro that expands to (one
would guess) some kind of implementation-specific extension
to C like `__attribute__((packed))''. If so, you''ll need to
consult your implementation''s documentation to find out
what''s going on, because it isn''t C.

Well, it *could* be C, if `PACKED'' expands to something
like `volatile'' or `const'' or nothing at all. In what
follows, I''ll assume that''s the case.



Not by the C language. The compiler has great freedom
in how it chooses to arrange bit-fields within a struct.
Different compilers will arrange things differently.



Not necessarily. In fact, it''s not guaranteed that
`color.color_u.color'' will have a valid value at all: unions
are frequently (ab)used for this sort of type punning, but
the language doesn''t promise that anything reasonable will
happen if you try it.

If you want to represent a color as three bits in a
`char'', you''re better off using bitwise operators with
pre-#defined (or enum-erated) masks:

typedef unsigned char color_t;
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_BLUE 4
#define COLOR_YELLOW (COLOR_GREEN | COLOR_RED)
...

--
Eric Sosman
es*****@acm-dot-org.invalid




这篇关于与压缩结构联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:39