如果是一个整数LT; - >指针转换实际上是正确的? [英] When is an integer<->pointer cast actually correct?

查看:111
本文介绍了如果是一个整数LT; - >指针转换实际上是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常见的民间传说说:


  • 该型系统中存在的一个原因。整数和指针是不同的类型,它们之间的铸造是在大多数情况下的弊端,可能表明一个设计错误,应该避免。

  • The type system exists for a reason. Integers and pointers are distinct types, casting between them is a malpractice in the majority of cases, may indicate a design error and should be avoided.

即使进行这样的演员阵容,没有假设应进行有关整数和指针(铸件的尺寸无效* INT 是使code失败的x64)的最简单的方法,而不是 INT 应该使用使用intptr_t uintptr_t形式 stdint.h

Even when such a cast is performed, no assumptions shall be made about the size of integers and pointers (casting void* to int is the simplest way to make the code fail on x64), and instead of int one should use intptr_t or uintptr_t from stdint.h.

知道了,时,它实际上有用执行这样的类型转换?

Knowing that, when is it actually useful to perform such casts?

(注:有一点短code便携性的价格并不能算作真正有用)

一个情况下,我知道了:

One case I know:


  • 部分的无锁算法多利用一个2 + - 字节指针神韵有一些冗余的事实。然后他们使用的指针作为布尔标志的最低位,例如。用具有一个适当的指令集的处理器,这可以消除对一个锁定机构的需要(如果指针和布尔标记是分开这将是必要的)。结果,
    (注意:这种做法甚至有可能在通过java.util.concurrent.atomic.AtomicMarkableReference的Java安全地完成)

  • Some lock-free multiprocessor algorithms exploit the fact that a 2+-byte-alligned pointer has some redundancy. They then use the lowest bits of the pointer as boolean flags, for instance. With a processor having an appropriate instruction set, this may eliminate the need for a locking mechanism (which would be necessary if the pointer and the boolean flag were separate).
    (Note: This practice is even possible to do safely in Java via java.util.concurrent.atomic.AtomicMarkableReference)

些什么?

推荐答案

我有时会投三分球为整数时,他们莫名其妙地需要一个哈希和的一部分。此外,我投他们整数做一些bitfiddling与他们在某些implemetnations它保证了指针总是有一个或两个备用比特离开,在那里我可以带左/右指针代替code AVL或RB树信息具有额外的成员。但是,这一切都是那么具体执行,我建议从来没有把它作为任何一种常见的解决方案。我又听见,有时危险的指针可以用这样的东西来实现。

I sometimes cast pointers to integers when they somehow need to be part of a hashsum. Also I cast them to integers to do some bitfiddling with them on certain implemetnations where it is guaranteed that pointers always have one or two spare bits left, where I can encode AVL or RB Tree information in the left/right pointers instead of having an additional member. But this is all so implementation specific that I recommend to never think about it as any kind of common solution. Also I heard that sometimes hazard pointers can be implemented with such a thing.

在某些情况下,我需要每个对象一个唯一的ID我一起例如通过服务器作为我的请求ID。根据上下文,当我需要节省一些内存,这是值得的,我用我的对象的地址作为这样一个id,通常都将它转换为整数。

In some situations I need a unique ID per object that I pass along to e.g. servers as my request id. Depending on the context when I need to save some memory, and it is worth it, I use the address of my object as such an id, and usually have to cast it to an integer.

在嵌入式系统的工作(如佳能相机,看到CHDK)经常有魔法addesses,所以(无效*)0xFFBC5235 或类似经常发现有太

When working with embedded systems (such as in canon cameras, see chdk) there are often magic addesses, so a (void*)0xFFBC5235 or similar is often found there too

编辑:

只是偶然(在我心中)在 pthread_self()它返回的pthread_t通常是一个typedef无符号整数。虽然内部,它是一个指向某个线程结构,重presenting有问题的线程。一般来说它可能在其他地方使用的不透明句柄。

Just stumbled (in my mind) over pthread_self() which returns a pthread_t which is usually a typedef to an unsigned integer. Internally though it is a pointer to some thread struct, representing the thread in question. In general it might used elsewhere for an opaque handle.

这篇关于如果是一个整数LT; - >指针转换实际上是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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