#include <iostream> int main() { int data = 5; int hidden_pointer = reinterpret_cast<int>(&data); int *newData_p = new (reinterpret_cast<void*>(hidden_pointer)); std::cout << *newData_p << std::endl; return 0; }
prints out a 5.
This assumes that the size of an integer is the same as that of a pointer. The above is an example of placement new. Initially, I was trying to find an example of using placement new instead of reinterpret_cast<>() for converting a hidden pointer back into a pointer, but instead ended up using reinterpret_cast<>() twice in the example.
No comments:
Post a Comment