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

前端 未结 8 1885
失恋的感觉
失恋的感觉 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: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.

提交回复
热议问题