Link to an array in memory

I have this memory layout:

0018FBD2 ?? ?? ?? ??    ?? ?? ?? ??
0018FBDA AA AA AA AA    BB BB BB BB  <- stuff I'm interested in
0018FBE2 ?? ?? ?? ??    ?? ?? ?? ??

In C, I would do:

int* my_array = (int*) 0x18FBDA;
my_array[0]; // access

However, I use C ++, and I would like to declare a link:

int (&my_array)[2] = (???) 0x18FBDA;

To use this:

my_array[0]; // access

But, as you can see, I do not know how to do this:

int (&my_array)[2] = (???) 0x18FBDA;

How should I do it? Is it possible?

+4
source share
2 answers

I find the concept of using an array reference a bit confusing, like tadman . But you can do it the same way as with any type, by dereferencing the pointer.

int (&my_array)[2] = *reinterpret_cast<int(*)[2]>(0x18FBDA);

Also, if you are planning on doing such a cast, don't let him seem innocent by performing a cast in the c style. Such a thing should stand out IMO.

+7
source

. ++ ( , , ):

template<typename T, size_t S>
using arr = T[S];

auto& my_arr = *reinterpret_cast<arr<int,2>*>(0x18FBDA);

auto& my_arr = *reinterpret_cast<int(*)[2]>(0x18FBDA);
+2

Source: https://habr.com/ru/post/1656868/


All Articles