位字段数组不受支持? [英] Bit field arrays unsupported?

查看:115
本文介绍了位字段数组不受支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我似乎不允许拥有位字段数组?

这有什么方法吗?那么好的方法呢?


我没有运气就快速尝试了typedef。


当然我应该被允许做任何我喜欢的事情。记忆......


感谢您的任何信息。

马特

解决方案
" ballpointpenthief" <马************* @ gmail.com>在消息中写道

news:11 ********************** @ b68g2000cwa.googlegr oups.com ...

您好,
我似乎不允许拥有位字段数组?
那是对的。不在C语言中。在C ++中你可以通过运算符重载来实现。

有没有办法解决这个问题?好方法呢?
编号但是位组可以提供大致相同的功能:


来自C-FAQ:

20.8:我如何实现集合或位数组?


A:使用char或int数组,使用几个宏来访问正确索引处的

所需位。以下是一些简单的宏来使用
与char数组一起使用:


#include< limits.h> / * for CHAR_BIT * /


#define BITMASK(b)(1<<((b)%CHAR_BIT))

#define BITSLOT( b)((b)/ CHAR_BIT)

#define BITSET(a,b)((a)[BITSLOT(b)] | = BITMASK(b))

#define BITTEST(a,b)((a)[BITSLOT(b)]& BITMASK(b))


(如果你没有< limits.h> ;尝试使用8作为CHAR_BIT。)


参考文献:H& S Sec。 7.6.7 pp.211-216。

我很快就尝试了typedef而没有运气。


不出意外。 char是C

语言中最小的可寻址单元。

当然我应该被允许用我的记忆做任何我喜欢的事情......


C可以做到这一点。如果你需要真正的数组语法,那么请使用

C ++。

感谢任何信息。
Matt



ballpointpenthief schrieb:

我似乎不允许拥有位字段数组?


这是一个问题吗?

这有什么方法吗?好方法呢?


编号无。

我很快就尝试了typedef而没有运气。


难怪。


指针指向的最小存储单位是

字节;对象的最小可能大小为1个字节。

unsigned char,例如,总是大小为1个字节。

如果将有效指针增加到1 ,它是
指向前一个字节的一个(通常是:下一个字节)。

由于数组是按指针引入的,它们不能

地址小于1个字节。 (a [i]实际上是

*((a)+(i))。)


你可以做什么,如果花哨的话,是下面的

之一:

1)声明一个包含位字段的结构类型和

声明这种结构类型的数组。 br />
2)如果n是你打算用于你的位域

类型的位数,则计算N = lcm(n,CHAR_BIT)并定义合适的

访问宏,它们为您提供或设置n位元素。要访问
(为
unsigned char [N / CHAR_BIT]创建访问宏,以便访问N / nn-bit

元素并从那里开始工作。


当然我应该被允许做任何我喜欢的事情......




如果您这么认为。大自然有一些限制,一些是你访问你的记忆的方式(在你使用的语言的语义中)和一些你的个人能力。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot /地址。


Michael Mair发布:

如果将指向unsigned char的有效指针增加1,它指向前一个字节的一个(通常是:下一个字节)。



我很好奇你的措辞...


在什么情况下它会指向下一个

字节以外的任何内容?


-


Frederick Gotham


Hello,
I don''t seem to be allowed to have arrays of bit fields?
Are there any ways round this? And what about good ways?

I tried typedef quickly with no luck.

Surely I should be allowed to do whatever I like with my memory...

Thanks for any info.
Matt

解决方案

"ballpointpenthief" <Ma*************@gmail.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...

Hello,
I don''t seem to be allowed to have arrays of bit fields? That''s right. Not in C. In C++ you can do it by operator overloading.
Are there any ways round this? And what about good ways? No. But bit sets can give approximately the same functionality:

From the C-FAQ:
20.8: How can I implement sets or arrays of bits?

A: Use arrays of char or int, with a few macros to access the
desired bit at the proper index. Here are some simple macros to
use with arrays of char:

#include <limits.h> /* for CHAR_BIT */

#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

(If you don''t have <limits.h>, try using 8 for CHAR_BIT.)

References: H&S Sec. 7.6.7 pp. 211-216.
I tried typedef quickly with no luck.
No surprise there. A char is the smallest addressible unit in the C
language.
Surely I should be allowed to do whatever I like with my memory...
C can sort of do it. If you need the real array syntax eye-candy, then use
C++ instead.
Thanks for any info.
Matt



ballpointpenthief schrieb:

I don''t seem to be allowed to have arrays of bit fields?
Is that a question?
Are there any ways round this? And what about good ways?
No. None.
I tried typedef quickly with no luck.
No wonder.

The smallest unit of storage a pointer can point to is a
byte; the smallest possible size of an object is 1 byte.
unsigned char, for example, always has size 1 byte.
If you increase a valid pointer to unsigned char by 1, it
points to one past the previous byte (often: the next byte).
As arrays are introduced in terms of pointers, they cannot
address anything smaller than 1 byte. (a[i] effectively is
*((a)+(i)).)

What you _can_ do, if the fancy strikes you, is one of the
following:
1) Declare a structure type containing the bit-field and
declare arrays of this structure type.
2) If n is the number of bits you intended for your bitfield
type, calculate N = lcm(n, CHAR_BIT) and define appropriate
access macros which give you or set the "n-bit element" to
be accessed (create access macros for
unsigned char[N/CHAR_BIT] in order to access N/n "n-bit
elements" and work from there).

Surely I should be allowed to do whatever I like with my memory...



If you think so. There are some limits imposed by nature, some
by the way you access your memory (among those the semantics
of the language you use) and some by your personal ability.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Michael Mair posted:

If you increase a valid pointer to unsigned char by 1, it
points to one past the previous byte (often: the next byte).


I''m curious about how you worded that...

In what circumstances would it point to anything other than "the next
byte"?

--

Frederick Gotham


这篇关于位字段数组不受支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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