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?

8
  • 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 but that could cause problems. Commented Oct 5, 2016 at 20:18
  • 2
    Is this embedded? You probably get a segfault if not Commented Oct 5, 2016 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. Commented Oct 5, 2016 at 20:21
  • 2
    @BaummitAugen, tell us how you really feel about C++ :) Commented Oct 5, 2016 at 20:27
  • 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 blog.stackoverflow.com/2010/11/… Commented Oct 5, 2016 at 20:42

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@krzaq, I find this more expressive. A reinterpret_cast in my code is a sign to myself I'm doing something risky which should be treated with care.
@krzaq, of course it's also a shorter way to get around this error
@krzaq, this entire post seems to me about shooting myself in the foot. Might as well do it with less keystrokes (a double static_cast is required, or at least a c-style cast to void* first) :)
That's what I get for using literal 0 to check if static_cast would allow this :)
@Martin It may be possible for you to get around the error with typedef int arr_t[2]
|
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.