[語法] 宣告參數以bit為單位的方式..

看板C_and_CPP (C/C++)作者 (eleghost)時間16年前 (2010/01/08 09:42), 編輯推噓2(208)
留言10則, 4人參與, 最新討論串1/1
看code時有看到這樣的寫法. .. struct _test { unsigned int a :1; unsigned int b :7; unsigned short c : 2; unsigned short d : 6; }; 上面是我自己胡亂舉例的, 所以想問說這樣寫法的解釋 unsinged int a :1; 表示 a被宣告4byte的空間, 但只佔用1bit ? 另外這樣的寫法是不是再搭配個 __attribute__((packed)) 會比較適合? 以上面的 struct 來看, 要是沒有加__attribute__ ... 使用sizeof就是4Byte, 如果有加, 就是2Byte. 謝謝. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.146.11.35

01/08 14:10, , 1F
印象中 __attribute__((packed)) 不是標準語法 所以要視情況
01/08 14:10, 1F

01/08 14:11, , 2F
使用 另外 使用類似__attribute__((packed))的語法會增加存
01/08 14:11, 2F

01/08 14:11, , 3F
取loading
01/08 14:11, 3F

01/08 14:25, , 4F
bit field和alignment是兩回事
01/08 14:25, 4F

01/08 14:25, , 5F
你最後那個4Bytes 2Bytes是alignment的關係
01/08 14:25, 5F

01/08 15:17, , 6F
回樓上 , 我是用gcc toolcahin所以可支援這類語法..
01/08 15:17, 6F

01/08 15:17, , 7F
請問holymars, bit-field是我上面的意思嗎?就是宣告4B
01/08 15:17, 7F

01/08 15:18, , 8F
但只用1 bit ? thanks!
01/08 15:18, 8F

01/08 23:13, , 9F
我所看過的__attribute__,文件他是說gcc和MSVC的語法不一樣
01/08 23:13, 9F

01/08 23:14, , 10F
不過那是講呼叫慣例的宣告~~ __attribute__((__cdecl)) ...
01/08 23:14, 10F
文章代碼(AID): #1BHes33w (C_and_CPP)
文章代碼(AID): #1BHes33w (C_and_CPP)