2017-08-13 4 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

' void *型hPoint = reinterpret_castは(ヒント)エラーをもたらすでしょう – user0042

+0

https://stackoverflow.com/questions/45657427/access-violation-casting-to-void-and-backに似たような質問 –

答えて

1

:あなたは、実際のメモリ位置hPointの値を見ている

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

。これは、uint64_t *に変換してから、その場所の値を取得するためです。

補足として、uint64_tは64ビットマシンでうまく動作しますが、このようなことを行う標準的な方法は、コンパイルしようとしているアーキテクチャのポインタのサイズであることが保証されているuintptr_tを使用することです。 `=>` void *型hPoint = reinterpret_castは (&hInt); `;あなたは非XXビットマシン上uintXX_tでコードをコンパイルした場合、コンパイラは

関連する問題