Is it safe to cast an int to void pointer and back to int again?

前端 未结 8 1887
失恋的感觉
失恋的感觉 2020-12-06 18:18

In C and/or C++: is it safe to cast an int to void pointer and back to int again?

Based on the question \"C++: Is it safe to cast pointer to int and later back to po

相关标签:
8条回答
  • 2020-12-06 19:13

    No. A void pointer is no different from any other pointer with respect to size. Hence it will run into exactly the same types of issues as other pointer types.

    0 讨论(0)
  • 2020-12-06 19:18

    In most modern-day commonplace machines, probably.

    However, I'd bet that there is some obscure compiler or configuration (say, a 16-bit addressed machine that uses 32-bit integer arithmetic) where that is not the case.

    A uintptr_t is guaranteed to hold both, though, so use that type if you want to.

    0 讨论(0)
提交回复
热议问题