Kar*_*ath 11 c++ byte language-lawyer
在这个答案中,int8_t*用于(字节)指针算术:
std::size_t offset = offsetof(Thing, b);
Thing* thing = reinterpret_cast<Thing*>(reinterpret_cast<int8_t*>(ptr) - offset);
Run Code Online (Sandbox Code Playgroud)
我过去总是使用char*,但评论真的很混乱,没有人回复,所以我发布了这个单独的问题.
是否char*有效,以及进行这些计算的首选方式?
您必须使用: 在指向非 an 的指针上char*使用 a 的行为是未定义的。强制转换可以被视为该规则的一个例外。reinterpret_castint8_t*int8_tchar*
C++14 之前版本,char可以是范围为 -127 到 +127 的 1 补码类型。int8_t必须是2的补码。即使是 C++14 及更高版本,我也不明白为什么这些类型是相关的:char仍然可以是有符号或无符号类型。
| 归档时间: |
|
| 查看次数: |
237 次 |
| 最近记录: |