Is this method of pointer tagging in C standard-compliant?

ぐ巨炮叔叔 提交于 2019-12-05 00:45:47

The only thing where I think you'd have to be more carefull, here, is your integer types. Don't use int:

  • the correct type for differences of pointers is ptrdiff_t.
  • the result of sizeof is size_t an unsigned type
  • You are doing % between size_t type and ptrdiff_t so the result is most probably size_t, so an unsigned value
  • converting that size_t to int is implementation defined (so not portable), usually it will just drop the high order bits

int will work for a long time but the day you use that with a very large array on a 64 bit processor you will regret it (after days of debugging)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!