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

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

问题描述

使用 intptr_t 作为通用存储(保存指针和整数值)而不是 void* 是个好主意吗?(如下所示:http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html)

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 往返不保证保持原始值;我猜 int -> intptr_t -> int 会做
  • void*intptr_t 上的指针运算都需要强制转换,因此这里没有任何优势
  • void* 表示在存储指针时较少显式转换,intptr_t 表示在存储整数值时较少转换
  • intptr_t 需要 C99
  • 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

我还应该考虑什么?

推荐答案

使用 intptr_t 作为通用存储(保存指针和整数值)而不是 void* 是个好主意吗?

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

没有

intptr_t 不保证存在.首先,正如您所注意到的,它是在 C99 中引入的.其次,实现不需要有足够大的整数类型来保存转换后的指针值而不会丢失信息.

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.

int 转换为 intptr_t 并返回不太可能丢失信息,但没有实际保证 intptr_tint 宽.

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.

任何指向对象或不完整类型的指针都可以转换为 void* 并再次返回而不会丢失信息.对于指向函数的指针没有这样的保证——但是任何指向函数的指针类型都可以转换为任何其他指向函数的指针类型并返回而不会丢失信息.(我指的是 C 标准;我认为 POSIX 提供了一些额外的保证.)

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).

有些 API 使用 void* 参数来允许传递任意数据;例如,请参见 POSIX pthread_create()功能.这可以通过将整数值转换为 void* 来滥用,但传递整数对象的地址更安全.

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 而不是 void*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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