与压缩结构联合 [英] union with packed struct

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

问题描述

大家好,

以下是示例代码段:

....

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吗?

解决方案

2005年3月3日22:10:31 -0800,ccwork < CC **** @ hotmail.com>写在

comp.lang.c:

大家好,
这是一个示例代码段:


实际上,这是一个非法的代码段。

...
typedef PACKED struct
{
union
{
PACKED struct


首先,typedef不完整所以你不能使用。

其次,你似乎试图定义一个包含

实例的类型作为成员。

{
char red:1;
char green:1;
char blue:1;
} color_bit;


另请注意,在1999版C标准之前,位字段成员唯一允许的类型是带符号或无符号的int。 C99

允许但不要求实现接受其他整数

类型。

char color;
} color_u;
} color_t;

color_t color;


下次尝试发布实际通过C

编译器而无错误的代码。

...
我的问题是: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

主页: http://JK-Technology.Com



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


ccwork写道:

大家好,
以下是一个示例代码段:
...
typedef PACKED struct


什么是PACKED?它不是C构造 - 它看起来好像它可能是一个#defined宏扩展到(一个

会猜测)某种实现 - 具体扩展

到C,如`__attribute __((packed))''。如果是这样,你需要

查阅你的实施文件,找出

发生了什么,因为它不是C.


好​​吧,它*可能是*,如果'PACKED''扩展为某种东西

就像`volatile''或'const''或者什么都没有所有。接下来是什么

,我会假设是这样的。

{
union
{
PACKED struct
{
char red:1;
char green:1;
char blue:1;
} color_bit;
char color;
} color_t;
} color_t;

color_t color;
...
我的问题是:color.color_u.color中的哪个位?反映
" color.color_u.color_bit.red"," color.color_u.color_bit.green"和
color.color_u.color_bit.blue?这个定义得很好吗?


不是C语言。编译器在如何选择在结构中排列位字段时有很大的自由度。

不同的编译器会以不同的方式排列。

In换句话说,如果我设置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吗?




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

`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
es ***** @ acm-dot-org .inva 盖子




如何分配" color.color_u.color_bit.red"到其他东西

比0和1?说color.color_u.color_bit.red = 20。会发生什么?

如何分配color.color_u.color_bit.red =(某些表达式)&&

(另一个表达式)?


实际上所有这些都是来自高级工程师的代码,我正在帮助他/她b $ b ...


Eric Sosman写道:

ccwork写道:

大家好,
这是一个示例代码段:
...
typedef PACKED struct



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

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

{
union
{
PACKED struct
{
char red:1;
char green:1;
char blue:1;
} color_bit;
char color;
} color_t;
} color_t;

color_t color;
...
我的问题是:color.color_u.color中的哪个位?反映
" color.color_u.color_bit.red"," color.color_u.color_bit.green"和
color.color_u.color_bit.blue?这个定义得好吗?



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

换句话说,如果我设置了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吗?



不一定。事实上,并不能保证
`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
es*****@acm-dot-org.inva 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?

解决方案

On 3 Mar 2005 22:10:31 -0800, "ccwork" <cc****@hotmail.com> wrote in
comp.lang.c:

Hi all,
Here is a sample code segment:
Actually, it is an illegal code segment.
...
typedef PACKED struct
{
union
{
PACKED struct
In the first place, the typedef is not complete so you can''t use.
Secondly, you appear to be trying to define a type that contains an
instance of itself as a member.
{
char red:1;
char green:1;
char blue:1;
} color_bit;
Also note that prior to the 1999 version of the C standard, the only
allowed types for bit-field members is signed or unsigned int. C99
allows, but does not require, implementations to accept other integer
types.
char color;
} color_u;
} color_t;

color_t color;
Next time try posting code that will actually pass through a C
compiler without error.
...
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


ccwork wrote:

Hi all,
Here is a sample code segment:
...
typedef PACKED struct
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.
{
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?
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.
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?



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


Hi,
How about assign "color.color_u.color_bit.red" to something other
than 0 and 1? Say color.color_u.color_bit.red=20. What will happen?
How about assign color.color_u.color_bit.red=(some expression) &&
(another expression)?

Actually all these are code from a senior engineer and I am helping
him...

Eric Sosman wrote:

ccwork wrote:

Hi all,
Here is a sample code segment:
...
typedef PACKED struct



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.

{
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?



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.

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?



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




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

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