1

I define an intermediary typedef in order to define a reference to a fixed size array.

typedef int CI[0x10];
CI& arr=*(CI*)mypointer;

writing it like that it permits me later on to use countof(arr)

I tried to write it in one statement and bellow is my failed attempt. I know that this is wrong as the "&" should be in both "int" and "[0x10]" and not on arr1 but is it possible to write it in one statement ;

int (arr1&)[classInfoN]=*(CI*)mypointer;
user3723779
  • 287
  • 1
  • 2
  • 12
  • 2
    Why not use `std::array`? – Neil Kirk Jan 13 '15 at 17:45
  • 1
    Because the above is used low level "hardware" code that doesn't want the additional overhead of std::array. – user3723779 Jan 13 '15 at 17:46
  • 3
    @user3723779: There is no extra overhead to `std::array`, after the optimizer has done its job. – Ben Voigt Jan 13 '15 at 17:47
  • 2
    @user3723779 Please show me the overhead of std::array. – Borgleader Jan 13 '15 at 17:48
  • there is some size overhead related to the structure kept by the array ( current position and max position ) – user3723779 Jan 13 '15 at 17:50
  • 2
    @user3723779: You're clearly thinking of `std::vector`, not `std::array`. – Ben Voigt Jan 13 '15 at 17:51
  • But tell it another way just to cut the rope. The array isn't created by the program itself ( or at least not by a part of the program controlled by me ). So instead I want to see and access an array structure created by an external entity. – user3723779 Jan 13 '15 at 17:52
  • Yes you are right std::vector in the implementation I have seen uses some pointers as the local variable. But also std::array stores somewhere the size of the array DYNAMICALLY. Instead by the method used before the size of the array is compiled. – user3723779 Jan 13 '15 at 17:54
  • 1
    Instead of "overhead" you should be worried about whether the layout of `std::array` is guaranteed to be identical to `T[N]`. Answer: The layout is compatible (lines up at the beginning), but [total size may not be the same](http://stackoverflow.com/q/24318303/103167). That's probably a good enough reason to avoid it. (So I gave an answer) – Ben Voigt Jan 13 '15 at 17:55
  • 1
    @user3723779: Nope, `std::array` doesn't store the size dynamically. The size is a template parameter, part of the compile time type. – Ben Voigt Jan 13 '15 at 17:56
  • But remember the question is not what I should do, but if I use [] if it is possible to avoid the "temporary" typedef. – user3723779 Jan 13 '15 at 17:57
  • @user3723779: My answer below proves that it is possible... – Ben Voigt Jan 13 '15 at 17:57
  • The size of the array is given in a field of a structure, so it is warranted to be correct. – user3723779 Jan 13 '15 at 17:58
  • 2
    You are misinformed, and not listening to anyone. – Lightness Races in Orbit Jan 13 '15 at 17:59
  • @LightnessRacesinOrbit Unfortunately ( for me ) you are right !!! I didn't understood what everybody was saying to me !!! It took me some hours to understand what you where speaking about !!! – user3723779 Jan 13 '15 at 19:09

2 Answers2

8

You've got the & in the wrong place. It always goes to the left of the identifier, just like your working example.

So:

int (&arr1)[0x10] = *reinterpret_cast<int (*)[0x10]>(mypointer);
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I believe that the "*" goes on the right of the type, so I thought that the "&" would go on the right of the type instead of the left of the variable. Please correct me accordingly. I was so simple ! – user3723779 Jan 13 '15 at 18:07
  • I just understood that the problem wasn't only on the left side of `=' but also on the right that is difficult too. – user3723779 Jan 13 '15 at 18:12
  • @user3723779: At first glance it seems like C++ uses left-to-right types, and type-variable declarations. But C and C++ don't. (Some languages do, in Java or C# you'd write `int[] var;` to declare an array) C and C++ instead use "declaration follows usage", which turns out to be the only thing that makes sense for really complicated types. Yes, `(int[0x10])& arr1 = ...` would make perfect sense in your case, but you don't want to do left-to-right typing for functions, class members, or (especially) multidimensional arrays. Declaration-follows-usage is trickier but very very consistent. – Ben Voigt Jan 13 '15 at 18:56
-1
// g++ test.cpp -std=c++11
#include <iostream>

int main() {
    int real_arr[0x10] = {1,2,3,4,5,6,7,8,9,0};

    typedef int (&arr_t)[0x10];
    arr_t arr = real_arr;

    for ( auto i = 0; i < 10; ++i ) {
        std::cout << arr[i] << std::endl;
    }
    return 0;
}
inetknght
  • 4,300
  • 1
  • 26
  • 52
  • 2
    You haven't answered his question, which is asking how to do it without first using typedef. – Ben Voigt Jan 13 '15 at 17:50
  • Sure I have. See how the typedef is different. Anywhere there's a declaration of a variable using a typedef, you can replace the declared type with the typedef's declaration. – inetknght Jan 13 '15 at 17:52
  • 1
    *is it possible to write it in one statement* if you have a typedef that's at least more than 1 -> You are not answering the question. – Borgleader Jan 13 '15 at 17:56
  • 1
    A shame really because this could have been a pretty good answer, to some other question! – Lightness Races in Orbit Jan 13 '15 at 18:00