2

I am trying to find a way to restore a reference to array through a pointer.

Look at the code:

const int rows = 2;
const int cols = 3;
int arr[rows][cols] = { {1,2,3}, {4,5,6} }; // step0

int(&arr_ref)[rows][cols] = arr; // create a a reference from array - OK

int* some_ptr = &arr[0][0]; // step1. create pointer to array - OK

//int(&arr_ref2)[rows][cols] = *some_ptr; // step2 (failed). restore the array reference from the pointer. impossible.

int(&arr_ref3)[rows][cols] = reinterpret_cast<int(&)[rows][cols]>(*some_ptr); // step2. ok???
  • step0: we have some array
  • step1: create a simple pointer from the array
  • step2: restore reference from the pointer. How to do it properly?

Does using reinterpret_cast lead to undefined behavior somehow if I'm absolutely sure about the size of the array.

Take harder. If we want to get an array of a different size and "shape", but it is for sure within the boundaries of the original array. Is the code below "legal"?

int arr2[6] = { 1,2,3,4,5,6 };
int* some_ptr2 = &arr2[0];
int(&arr2_ref)[2][2] = reinterpret_cast<int(&)[2][2]>(*some_ptr2); // ok???

UPD: If the array contains a complex objects with inheritance, virtual functions etc will it work? Will it be a reliable solution?

dimasafo
  • 147
  • 5
  • 1
    Why would you ever cast it through a pointer? Don't do that. Nevertheless, it is an interesting and well worded question. – Mooing Duck Jun 08 '20 at 19:07
  • 1
    For the last line of your first code snippet, `clang-cl` considers it UB: **warning : reinterpret_cast from 'int' to 'int (&)[2][3]' has undefined behavior [-Wundefined-reinterpret-cast]** but `MSVC` doesn't give even a peep. (And similarly for the second code snippet.) – Adrian Mole Jun 08 '20 at 19:11
  • Related: [Reshaping a 1-d array to a multidimensional array](https://stackoverflow.com/questions/15283523/reshaping-a-1-d-array-to-a-multidimensional-array). – Evg Jun 08 '20 at 19:13
  • @Adrian That some compiler doesn't complain does not make it any less UB. Compilers are *not* obliged to diagnose UB. – Jesper Juhl Jun 08 '20 at 19:14
  • @JesperJuhl I agree. Just pointing out what the tools I use had to offer. – Adrian Mole Jun 08 '20 at 19:15
  • Does this answer your question: [Pointer interconvertibility vs having the same address](https://stackoverflow.com/questions/47924103/pointer-interconvertibility-vs-having-the-same-addre) – n. m. could be an AI Jun 08 '20 at 19:15

1 Answers1

2

You have to cast:

int(&arr_ref2)[rows][cols] = (int(&)[rows][cols])some_ptr;

When you declare the pointer int* some_ptr = &arr[0][0]; you are declaring a pointer to the first int from the array: pointer to one int. So you have to cast the pointer back to the array. And don't reference the pointer (with *some_ptr).

Manuel
  • 2,526
  • 1
  • 13
  • 17
  • Please explaint it. >> So you have to cast the pointer back to the array. 1. I dereference the pointer (to get a value) 2. Then I cast it to reference. It does not work without * (at least in MSVC), but with * does. – dimasafo Jun 09 '20 at 19:12