我正在尝试对地址执行算术运算。我需要确保它位于 4 字节字边界上。这需要我获取指针的无符号整数表示来执行一些数学运算。我已经这样做了:
//memStartAddr is of type void* (from an external API function)
data32 tempAddr = reinterpret_cast<data32>(memStartAddr);
//adjust pointer to the next word boundary if needed
tempAddr = wordAlign(tempAddr);
//memStartAddress is a class variable of type unsigned char*
memStartAddress = reinterpret_cast<data8*>(tempAddr);
//calculate bytes lost due to above adjustment
data32 delta = (tempAddr - reinterpret_cast<data32>(memStartAddress));
//Take advantage of integer arithmetic to ensure usable size is a multiple
//of 4 bytes. Remainders are lost. Shrinks usable size if necessary.
memSize = ((size - delta) / 4) * 4;
这一切在我的测试中都有效,但是,使用 reinterpret_cast 被认为是一种禁止的做法。还有另一种方法可以做到这一点吗?或者这里的规则是一个例外?