Martin Martin - 3 months ago 11
C++ Question

Reference to 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'm using C++, and I'd like to declare a reference:

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

In order to use like this:

my_array[0]; // access

But as you can see, I don't know how to cast it:

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

How should I do this? Is it even possible?


I find the notion of using an array reference a bit convoluted, like tadman mentioned. But you can do it as you'd do with any type, by dereferencing a pointer.

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

Also, if you are going to do such a cast, don't let it appear innocent by doing a c-style cast. Such a thing should stand out IMO.