0
說CMSG_ALIGN(i)
的值是8的倍數的最小值是否正確>=
如果i
是一個無符號整數變量?CMSG_ALIGN宏的值是什麼
#include <stdio.h>
int main(void)
{
int i;
for (i=0; i<50; ++i) {
printf("%d\n", CMSG_ALIGN(i));
}
}
輸出I得到:
/* i CMSG_ALIGN(i)
*
* 0 0
* [1,8] 8
* [9,16] 16
*/
謝謝!我測試了sizeof(struct cmsghdr),值是16.三個成員是cmsg_len,cmsg_level和cmsg_type。爲什麼sizeof(struct cmsghdr)不是12? – hel
[struct padding](https://en.wikipedia.org/wiki/Data_structure_alignment)。這是爲了對齊而完成的。請參閱:[爲什麼不是一個結構的sizeof等於每個成員的sizeof之和?](http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to -the-sum-of-sizeof-of-member) –
在我的問題sizeof(struct cmsghdr)中,成員'cmsg_len'是'socklen_t',4B(總共12B爲3個成員),但是我發現'cmsg_len'是我的系統中的'size_t',8B(3個成員共16B)。我在目錄/ usr/include中使用命令vi -t cmsghdr – hel