[問題] struct bit field

作者: gn00618777 (非常念舊)   2016-05-19 23:33:38
不好意思請問一下...我在網路上範例都是直接說出 struct 大小為 4 byte
struct {
unsigned int widthValidated : 1;
unsigned int heightValidated : 1;
} status;
請問有比較詳細的算法嗎?
我的想法是
總共兩個 bit ,但一次存取是 4 byte ,所以後面 padding 30 bit
求出大小為 4 byte
不知道我這樣想對不對....
感謝
作者: Caesar08 (Caesar)   2016-05-19 23:48:00
因為C++規定,sizeof不能是0既然不能是0,那最少也得是1阿,對不起,我看錯題目了...
作者: bibo9901 (function(){})()   2016-05-19 23:55:00
我以為這意思是: 只使用unsigned int 的 2 個bit ?
作者: dijkstra (邪惡數學家)   2016-05-20 01:03:00
同上
作者: gn00618777 (非常念舊)   2016-05-20 05:32:00
= = 那既然只用了 2 個 bit ,她怎出現 4 byte 呢?http://goo.gl/IgVVpP那這樣我的想法應該是對的!
作者: Caesar08 (Caesar)   2016-05-20 10:37:00
另外,我不是很懂為什麼需要知道bit field的大小bit field的實際大小是implementation-defined
作者: james1022jk (雁)   2016-05-20 11:19:00
他應該是想要知道bit field怎麼影響struct的大小
作者: gn00618777 (非常念舊)   2016-05-20 18:33:00
是的,我是想知道大小。http://goo.gl/zl5D7q 它的第一句有解答到我的問題,那這樣一切都瞭了~!
作者: bibo9901 (function(){})()   2016-05-21 00:43:00
所謂 implementation-defined 就是不同編譯器可能有不同作法
作者: Caesar08 (Caesar)   2016-05-21 09:58:00
他好像不是很懂甚麼是implementation-defined...

Links booklink

Contact Us: admin [ a t ] ucptt.com