Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to array in memory [duplicate]

Tags:

c++

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?

like image 775
Martin Avatar asked Jan 06 '23 06:01

Martin


1 Answers

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.

like image 71
StoryTeller - Unslander Monica Avatar answered Jan 07 '23 18:01

StoryTeller - Unslander Monica