1

I need to create a 2 dimensional array with dynamically allocated memory (both dimensions will be set at runtime), but for efficiency purposes I want to create a one dimension version of it with a contiguous allocated memory, and yet again somehow want to use it as a two dimensional array. Is there a way to use casting to implement this. The usage I want to implement is as below, which clearly does not work. Best,

int size = 4;
int* arr = new int[size];
arr[0] = 10;
arr[1] = 11;
arr[2] = 12;
arr[3] = 13;
cout << (int[][2])arr[1][1] << endl;
1
  • Why do you intend to manage the memory explicitly? It's not C++ idiomatic to use naked arrays. For your case there would be boost::multidim (though I know many people frown upon boost, for various reasons). boost.org/doc/libs/1_57_0/libs/multi_array/doc/user.html Commented Dec 16, 2014 at 11:24

3 Answers 3

2

While I think Codor's solution is in best compliance with the coding style of C++, I would like to share my solution done in C-style.

#include <iostream>

typedef int (*pint2)[2]; //pint2 is a pointer to an array of 2 ints

int main()
{
    int arr[4] = { 0, 1, 2, 3 };
    pint2 parr = (pint2)arr;
    std::cout << parr[0][0] << std::endl;
    std::cout << parr[0][1] << std::endl;
    std::cout << parr[1][0] << std::endl;
    std::cout << parr[1][1] << std::endl;

    getchar();
    return 0;
}

Hope this helps! (or at least that you found this interesting :/)

EDIT: And for arrays of variable lengths!

#include <iostream>

int main()
{
    int arr[12] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    const int sizeofarr = sizeof(arr)/sizeof(arr[0]); //Number of elements in array

    {
        const int numofrows = 2; //Number of rows
        const int numofcolumns = sizeofarr/numofrows; //Number of columns
        typedef int (*pinta)[sizeofarr/numofrows]; //A 2D array of columns of 2

        pinta parr = (pinta)arr;
        for(int i = 0; i < numofrows; ++i)
            for(int j = 0; j < numofcolumns; ++j)
                std::cout << parr[i][j] << std::endl;
    }

    {
        const int numofrows = 3; //Number of rows
        const int numofcolumns = sizeofarr/numofrows; //Number of columns
        typedef int (*pinta)[sizeofarr/numofrows]; //A 2D array of columns of 3

        pinta parr = (pinta)arr;
        for(int i = 0; i < numofrows; ++i)
            for(int j = 0; j < numofcolumns; ++j)
                std::cout << parr[i][j] << std::endl;
    }

    getchar();
    return 0;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Nard, none of the dimensions are known at compile time so I cannot use typedef int (*pint2)[2]. Yet, it is interesting.
@Efe Edited to your liking. :D
And as per usual, they must be homogeneous.
this might really help Nard. Cheers.
That makes me really happy :D Cheers!
|
2

Since this is C++, you could wrap the one-dimensional array (which is necessary due to the locality requirement) and overload operator() to permit two-dimensional acccess using some suitable index calculation.

1 Comment

So there seems to be no solution using builtin type casting. I shall consider overloading method.
0

g++ compiles the following slightly changed line (operator precedence) with -fpermissive:

cout << ((int[][2])arr)[1][1] << endl;

I assume that it is UB (anybody?) but I also assume that it will work just fine on all "common" platforms.

4 Comments

somehow it did not in osx.
g++ (GCC) 4.8.3 Target: i686-pc-cygwin; also -fpermissive was needed. But I find Nard's cast much better. It is what one should do (if one wants to do it).
Oh; you mean it compiled but crashed?
I asumme "that it will work just fine on all "common" platforms" ... with gcc ;-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.