将指针转换为int并返回到键入的对象 [英] Convert pointer to int and back to typed object

查看:55
本文介绍了将指针转换为int并返回到键入的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种简单的方法将指针转换为int,然后再转换回指针.我需要它作为可移植性的int,因为int比指针更容易在项目中随身携带.

I need a simple way to convert a pointer to an int, and then back to the pointer. I need it as an int for portability, since an int is easier to carry around in my project than a pointer.

启动播放器并返回指针:

Start player and return pointer:

SoundPlayer* player = new FxPlayerTiny();
return (int)player; // this works, but is it correct?

使用指针int停止播放器:

Stop player using pointer int:

FxPlayerTiny* player = static_cast<FxPlayerTiny*>((int*)num); // this gives me an error
FxPlayerTiny* player = (FxPlayerTiny*)((int*)obj); // this works, but is it correct?
delete player;

推荐答案

最好的办法是修复您的项目,使其可以处理指针.整数和指针是两个不同的东西.您可以来回转换,但是如果不小心,这种转换可能会丢失信息.

The best thing to do is to fix your project so it can deal with pointers. Integers and pointers are two different things. You can convert back and forth, but such conversions can lose information if you're not careful.

根据实现和基础平台,将指针值转换为 int 并再次返回很容易丢失信息.例如,在 int 上有小于指针的系统.如果您有32位的 int 和64位的指针,则将指针转换为 int 并再次返回将几乎肯定会给您一个无效的指针.

Converting a pointer value to int and back again can easily lose information, depending on the implementation and the underlying platform. For example, there are systems on int is smaller than a pointer. If you have 32-bit ints and 64-bit pointers, then converting a pointer to an int and back again will almost certainly give you an invalid pointer.

long unsigned long 的宽度很可能足以容纳转换后的指针值而不会丢失信息;我从来没有在没有的系统上工作过.(就我个人而言,我倾向于使用无符号类型,但出于任何确实很好的理由,它都不应该;两者都应同样有效.)

It's very likely that long or unsigned long is wide enough to hold a converted pointer value without loss of information; I've never worked on a system where it isn't. (Personally, I tend to prefer unsigned types, but not for any really good reason; either should work equally well.)

例如,您可以写:

SoundPlayer* player = new FxPlayerTiny();
return reinterpret_cast<unsigned long>player;

,然后使用 reinterpret_cast,SoundPlayer *> unsigned long 值转换回指针.

and convert the unsigned long value back to a pointer using reinterpret_cast,SoundPlayer*>.

较新的实现提供了typedef uintptr_t intptr_t ,它们是无符号和有符号整数类型,保证可以正常运行往返指针到整数到指针的转换.它们是在C99中引入的,并且可选地< stdint.h> 标头中定义.(指针大于任何整数类型的实现不会定义它们,但是这种实现很少.)C ++在2011年标准中采用了它们,并在< cstdint>中定义了它们.标头.但是Microsoft Visual C ++从2010版本开始就支持它们.

Newer implementations provide typedefs uintptr_t and intptr_t, which are unsigned and signed integer types guaranteed to work correctly for round-trip pointer-to-integer-to-pointer conversions. They were introduced in C99, and optionally defined in the <stdint.h> header. (Implementations on which pointers are bigger than any integer type won't define them, but such implementations are rare.) C++ adopted them with the 2011 standard, defining them in the <cstdint> header. But Microsoft Visual C++ does support them as of the 2010 version.

此保证仅适用于普通指针,不适用于函数指针或成员指针.

This guarantee applies only to ordinary pointers, not to function pointers or member pointers.

因此,如果必须执行此操作,则可以编写:

So if you must do this, you can write:

#include <cstdint>
SoundPlayer* player = new FxPlayerTiny();
return reinterpret_cast<std::uintptr_t>player;

但是首先,请考虑一下. new FxPlayerTiny()为您提供了一个指针值,您将需要一个指针值.如果有可能,只需将其保留为指针即可.将其转换为整数意味着您必须决定要使用几种技术中的哪一种,并且必须跟踪整数值应该代表哪种指针类型.如果您犯了一个错误,或者使用了一个不够大的整数类型,或者忘记了所存储的指针类型,编译器可能就不会对此发出警告.

But first, consider this. new FxPlayerTiny() gives you a pointer value, and you're going to want a pointer value. If at all possible, just keep it as a pointer. Converting it to an integer means that you have to decide which of several techniques to use, and you have to keep track of which pointer type your integer value is supposed to represent. If you make a mistake, either using an integer type that isn't big enough or losing track of which pointer type you've stored, the compiler likely won't warn you about it.

也许您有充分的理由要做这件事,但是如果您可以只将指针存储为指针,那么您的生活就会轻松很多.

Perhaps you have a good reason for needing to do that, but if you can just store pointers as pointers your life will be a lot easier.

这篇关于将指针转换为int并返回到键入的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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