0

How to create a 2d array using dynamic memory allocation in c++?

Maze(int c=10){
   const int m=c;
   a=new int[m][m];
  }

void main(){
  Maze(12);
}
1
  • 1
    Pleas don't SHOUT at us. It is considered to be vulgar.
    – Stephen C
    Commented Feb 28, 2015 at 5:29

3 Answers 3

2

std::vector is the typical way to have a dynamically allocated array in C++. You can have a vector of vectors to make it two-dimensional. Here's an example:

std::vector<std::vector<int>> a(m,std::vector<int>(m));

If you want it inside a class:

struct Maze {
    std::vector<std::vector<int>> a;

    Maze(int m) : a(m,std::vector<int>(m)) { }
};
0
1

Easily - using multiplication. Also I suggest using reference to array because in this way you specify the type more explicitly then using a pointer to it's first element. I'm actually amazed why this isn't the type most programmers use. Perhaps because they're lazy and the type is complex ;).

void Maze(int c=10) {
   const int m=c;
   int (&a)[0][0] = *(int (*)[0][0])new int[/*numbers of rows*/ m * sizeof(int) * m /* number of colums on each row*/];
}

Here 'a' is an reference to the newly created array. As types aren't dynamic in 'C++' language we assume that it has zero elements on each of it's dimensions. But of-course we can access more then 0.

Now if you have a function with parameter of type 2 dim array it will look like this:

void func(int (&_2dimarray)[0][0]) ;

Or if you want to return it from your 'Maze' you could write:

int (&Maze(int c=10))[0][0] {
   const int m=c;
   int (&a)[0][0] = *(int (*)[0][0])new int[/*numbers of rows*/ m * sizeof(int) * m /* number of colums on each row*/];
   return a;
}

Life example.

But of-course the easiest way is using 'std::vector' which however can have performance cost on some compilers while the built-in array will more surely run fast everywhere.

EDIT: The explanation is simple - the 'new []' can be thought as a function like:

template<class T>
T *operator new T[] (std::size_t);

Your instance of it:

a=new int[m][m];

Can also look like this (illustrative)

a=operator new int[m][](m);

Which fulfills 'T' with 'int[m]'.

This is illegal because 'int[m]' is not valid type. 'C++' supports only static types and this is not such because the length of the array can't be determined during compile-time as 'm' is not a constant. The last 'm' is a function parameter to 'operator new[]'.

Yep I also think this construct isn't the most elegant yet but this is the life.

0

There are two approaches. If the size of the internal one-dimensional subarray is a constant value known at compile time then you can write

const size_t N = 10;

int ( * )[N] Maze( size_t n = N )
{
    return new int[n][N];
}

int main()
{
    int ( *a )[N] = Maze( 12 );

    //...

    delete [] a;
}

If it is not a constant then you need to allocate a one-dimensional array of pointers to one-dimensional arrays. For example

const size_t N = 10;

int ** Maze( size_t n = N )
{
    int **p = new int *[n];

    for ( size_t i = 0; i < n; i++ ) p[i] = new int[n];

    return p;
}

int main()
{
    int **a = Maze( 12 );

    //...

    for ( size_t i = 0; i < 12; i++ ) delete [] a[i];
    delete [] a;
}

Also you could use smart pointers as for example std::unique_ptr.

The other approach is to use standard container std::vector<std::vector<int>>