2017-08-13 127 views
-1

我尝试以下方法句柄转换为void *,然后返回到处理以下方式转换uint64_t中为void *和背部

uint64_t hInt = 154071804376; //assume this is a valid memory location 

void* hPoint = reinterpret_cast<void*>(hInt); 

uint64_t hIntBack = *static_cast<uint64_t*>(hPoint); unable to recover hInt here, getting some other number 140727986249696 

但是,如果我这样做,我能恢复hInt

uint64_t hIntBack = reinterpret_cast<uint64_t>(hPoint) 

我不知道我明白这两种方法之间的区别。

+0

'无效* H对准=的reinterpret_cast (HINT);'=>'无效* H对准=的reinterpret_cast (&hInt);' – user0042

+0

类似的问题到https://stackoverflow.com/questions/45657427/access-violation-casting-to-void-and-back –

回答

1

在此代码:

uint64_t hIntBack = *static_cast<uint64_t*>(hPoint); unable to recover hInt here, getting some other number 140727986249696 

你实际上是在寻找的内存位置hPoint值。这是因为您将其转换为uint64_t *,然后在该位置获取该值。

作为一个方面说明,虽然uint64_t可以在64位机器上正常工作,但是执行此类操作的标准方法是使用uintptr_t,它保证是您正在编译的体系结构的指针大小。如果你要编译一个非XX位机器上uintXX_t代码时,编译器会带来错误

相关问题