Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我是否可以安全地假设数据成员 ( offsetof(mystruct, myfield)) 的偏移量在数值上等于使用 检索的成员指针的原始值&mystruct::myfield,还是依赖于实现?
offsetof(mystruct, myfield)
&mystruct::myfield
不; 没有指定指向成员的实现,也没有定义转换来获取“原始”值。
形式上,它当然是依赖于实现的。在现实生活中,是的,指向数据成员指针的最流行的实现是(或基于)plan-and-simple offset,即与offsetof求值相同的东西。
offsetof
一些实现使用精确的偏移量,使用0xFFF...模式来表示空指针值。一些实现为空指针值保留0x000...模式,同时将所有“非空”偏移量增加1.
0xFFF...
0x000...
1