4

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?

Martin
  • 1,065
  • 8
  • 13
  • Why are you thinking C++ would handle this differently? If it's a C-style array, use C notation. You want a reference. You've got a pointer. You're going to have to deal with pointers. You can [de-reference if you want](http://stackoverflow.com/questions/10172735/how-to-cast-convert-pointer-to-reference-in-c) but that could cause problems. – tadman Oct 05 '16 at 20:18
  • 2
    Is this embedded? You probably get a segfault if not – Ryan Oct 05 '16 at 20:19
  • 1
    Just use the pointer, everyone knows how that works anyways. If you want that to look more C++, call the pointer an "iterator" and, if you feel like it, use another past-the-end pointer as an end iterator. – Baum mit Augen Oct 05 '16 at 20:21
  • If you are bored enough, you can also implement some `array_view` class with a custom `operator[]` to wrap the original array in. – Baum mit Augen Oct 05 '16 at 20:22
  • `reinterpret_cast` seems like the proper tool for the task. Considering what the question is, I assume this is ring-0 code or embedded. – asu Oct 05 '16 at 20:23
  • 2
    @BaummitAugen, tell us how you *really* feel about C++ :) – StoryTeller - Unslander Monica Oct 05 '16 at 20:27
  • @StoryTeller Well, it's better than ANSI C. ;) – Baum mit Augen Oct 05 '16 at 20:36
  • 1
    Not sure that this question is really a duplicate of the other one: this one is very specific about using a reference for an array at a specific physical location. The other one is very general about legality of array reference casting, and someone looking for having an array at a fixed location might not recognize that it answers his question. See http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – Christophe Oct 05 '16 at 20:42

2 Answers2

7

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.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
2

I would go with C-style pointer. But if you really want the C++ way (it isn't, not any more than what you presented in the question):

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

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

which is just a clearer notation for

auto& my_arr = *reinterpret_cast<int(*)[2]>(0x18FBDA);
krzaq
  • 16,240
  • 4
  • 46
  • 61