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

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

    No. There might be certain circumstances where it appears to work for a certain compiler&settings, and then two years later you spend weeks debugging that something changed and the conversion no longer works as expected.

    If you just design your code in a way that doesn't need this sort of behavior (best case avoids use of such conversion at all, worst case use char[]) then you won't have to worry about obscure bugs in the future.

提交回复
热议问题