利用使用intptr_t而不是无效*? [英] Using intptr_t instead of void*?

查看:234
本文介绍了利用使用intptr_t而不是无效*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是个好主意,用使用intptr_t 作为一个通用的存储(持有指针和整数值),而不是无效* ? (因为在这里看到:<一href=\"http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html\">http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html)

对于我已经阅读:


  • INT - > 无效* - > INT 往返不保证持有原有价值;我猜 INT - > 使用intptr_t - > INT 将尽

  • 在这两个指针算术无效* 使用intptr_t 要求的演员,所以没有得到好处此处

  • 无效* 存储指针时意味着更少的显式转换,使用intptr_t 意味着更少的演员存储整数值,当

  • 使用intptr_t 要求C99

我应该采取什么其他考虑?


解决方案

  

这是个好主意,用使用intptr_t 作为一个通用的存储(持有指针和整数值),而不是无效*


没有。

使用intptr_t 不保证存在。首先,你注意,它是在C99引入。第二,实现并不需要具有整数类型足够大,以容纳转换指针值而不丢失信息。

转换的 INT 使用intptr_t 和回复是的不会的丢失信息,但有以没有实际的保证使用intptr_t INT 宽。

如果你想存储指针值,将其存储在指针的对象。这就是指针对象是

任何指向对象或不完全类型可以被转换为无效* ,然后再返回而不会丢失信息。有对函数指针没有这样的保证 - 但任何指针到功能类型可以转换为任何其它指针到功能型和背面而不会丢失信息。 (我指的是C标准。我认为POSIX提供了一些额外的担保)

如果你想存储一个整数或同一对象的指针值,你应该做的第一件事就是重新考虑你的设计。如果你已经这样做了,并得出结论认为你真的想这样做,可以考虑使用一个联盟(并保持你已存储最近什么样的价值仔细记录)。

有使用一个无效* 参数来允许任意数据传递的API;见,例如,POSIX 在pthread_create() 功能。这可以通过铸造一个整型值无效* 被滥用,但它的安全通过的地址的整数对象

Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void*? (As seen here: http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html)

For what I've already read:

  • int -> void* -> int roundtrip is not guaranteed to hold original value; I guess int -> intptr_t -> int will do
  • pointer arithmetics on both void* and intptr_t require casts, so none gets advantage here
  • void* means less explicit casts when storing pointers, intptr_t means less casts when storing integer values
  • intptr_t requires C99

What else should I take into consideration?

解决方案

Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void*?

No.

intptr_t is not guaranteed to exist. First, as you note, it was introduced in C99. Second, implementations are not required to have an integer type big enough to hold converted pointer values without loss of information.

Converting an int to intptr_t and back is unlikely to lose information but there's no actual guarantee that intptr_t is wider than int.

If you want to store pointer values, store them in pointer objects. That's what pointer objects are for.

Any pointer to an object or incomplete type can be converted to void* and back again without loss of information. There is no such guarantee for pointers to functions -- but any pointer-to-function type can be converted to any other pointer-to-function-type and back without loss of information. (I'm referring to the C standard; I think POSIX provides some additional guarantees.)

If you want to store either an integer or a pointer value in the same object, the first thing you should do is re-think your design. If you've already done so, and concluded that you really do want to do this, consider using a union (and keeping careful track of what kind of value you've stored most recently).

There are APIs that use a void* argument to allow arbitrary data to be passed; see, for example, the POSIX pthread_create() function. This can be abused by casting an integer value to void* but it's safer to pass the address of an integer object.

这篇关于利用使用intptr_t而不是无效*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆