3

The question is more specifically this one: How many bit-field entries can I add in a structure?

For example:

struct SMyStruct
{
   unsigned int m_data1 : 3;
   unsigned int m_data2 : 1;
   unsigned int m_data3 : 7;
   // ...
   unsigned long long m_datan : 42;
};

May the total of bits exceed 32 or 64 (or whatever is the system architecture)?

4

2 に答える 2

3

制限はありません。重要なことは、ビットフィールドの数がデータ型のビット数よりも大きくならないことです。たとえば、次のようになります。

typedef struct _Structure {
  int field1:32;  // OK
  int field2:40;  // Error, int is 32 bit size
  char field3:4;  // OK
  char field4:9;  // Error, char is 8 bit size
} Structure;

データ型のサイズ、ビット フィールドの数、およびエンディアンは、ハードウェア/コンパイラに依存します。

于 2013-11-12T16:06:10.983 に答える
2

C 標準では、struct. C 2011 (N1570) 5.2.4.1 1:

実装は、次の制限のすべてのインスタンスを少なくとも 1 つ含む少なくとも 1 つのプログラムを変換および実行できる必要があります: ... 1 つの構造体または共用体に 1023 メンバー ...</p>

1023 メンバー (およびその他の制限) を持つプログラムを少なくとも 1 つ変換できる限り、実装はメンバーが少ない一部のプログラムを変換できない場合があります。中程度の品質の実装であれば、ビットフィールドを含む妥当な数のメンバーを処理できます。

于 2013-11-12T19:20:47.657 に答える