DWORD-aligned

系统:Windows8.1 64bit CS

这两天正好看到InitializeACL()  msdn.microsoft.com/en-us/library/windows/desktop/aa378853(v=vs.85).aspx

其中nAclLength [in]要求". In addition, this value must be DWORD-aligned. For more information about calculating the size of anACL, see Remarks.",而后面的例子是这么做align的

// Align cbAcl to a DWORD.
    cbAcl = (cbAcl + (sizeof(DWORD) - 1)) & 0xfffffffc;

那到底什么是aligned呢,为什么cbAcl要那么计算呢??


幸好有【ri_aje】的回答:

 cbAcl = (cbAcl + (sizeof(DWORD) - 1)) & 0xfffffffc;
cbAcl要被DWORD-aligned的意思就是能被4整除     在windows 32位、64位上DWORD都是4字节(64位上有个单独的DWORD64),即能被4整除。

http://stackoverflow.com/questions/39419/visual-c-how-large-is-a-dword-with-32-and-64-bit-code

任意数X要做到能被DWORD/4整除要做2步(类似可以扩展到WORD,DWORD64吧)

1.  X=X+(sizeof(DWORD)-1)    假设X∈【N*4 , (N+1)*4 ),N属于正整数。 X加上sizeof(DWORD)-1的目的是为了得到不小于X的并能被4整除的N*4或(N+1)*4

2. X=X & 0xfffffffc   与0xfffffffc的目的就是让X能被4整除。 若X==N*4,那最后我们得到N*4; 若X>N*4,那最后我们得到(N+1)*4


根据MSDN的DWORD-aligned定义  http://msdn.microsoft.com/en-us/library/aa369366%28v=vs.85%29.aspx

Each data structure in a value list must begin on a DWORD boundary. This means that the distance (in bytes) between the start of the value list buffer and the start of any data structure is always an even multiple of sizeof(DWORD).

这里要注意的是

1.   “begin on a DWORD boundary.”:DWORD是4字节,故DWORD bounday就是能被4整除的地方

2. "an even multiple of" :这里even不是偶数的意思,而是作为adv. "used to emphasize something unexpected or surprising",强调multiple of,即整数倍sizeof(DWORD)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值