Например, у меня есть массив из 32 целые числа (
Код: Выделить всё
arr
Код: Выделить всё
arr[30]
Код: Выделить всё
p). Используя memcpy, я могу написать:
// store the pointer
auto address = reinterpret_cast(p);
std::memcpy(&Memory[30], &address, sizeof(address));
// retrieve the pointer
int *ptr;
std::memcpy(&ptr, &Memory[30], sizeof(ptr));
// test - succeed
assert(ptr == p);
Код: Выделить всё
// store the pointer
auto address = reinterpret_cast(p);
Memory[30] = address[0];
Memory[31] = address[1];
// retrieve the pointer
int *ptr;
std::memcpy(&ptr, &Memory[30], sizeof(ptr));
// test - failed
assert(ptr == p);
Подробнее здесь: https://stackoverflow.com/questions/792 ... pointer-to