Related Articles

# How to create a dynamic 2D array inside a class in C++ ?

• Difficulty Level : Hard
• Last Updated : 29 May, 2017

Suppose we want to create a class for Graph. The class stores adjacency matrix representation of the graph. Therefore, our class structure would be something like below.

 `class` `Graph ``{``  ``int` `V; ``  ``int` `adj[V][V];  ``// This line doesn't work``   ` `  ``/* Rest of the members */``};`` ` `int` `main()``{``}`

Output :

```error: invalid use of non-static data
member 'Graph::V'.
```

Even if we make V static, we get error “array bound is not an integer constant”

C++ doesn’t allow to create an stack allocated array in a class whose size is not constant. So we need to dynamically allocate memory. Below is a simple program to show how to dynamically allocate 2D array in a C++ class using a class for Graph with adjacency matrix representation.

 `// C++ program to show how to allocate dynamic 2D``// array in a class using a Graph example.``#include``using` `namespace` `std;`` ` `// A Class to represent directed graph``class` `Graph``{``    ``int` `V;    ``// No. of vertices`` ` `    ``// adj[u][v] would be true if there is an edge``    ``// from u to v, else false``    ``bool` `**adj;`` ` `public``:``    ``Graph(``int` `V);   ``// Constructor`` ` `    ``// function to add an edge to graph``    ``void` `addEdge(``int` `u, ``int` `v)  { adj[u][v] = ``true``; }``    ``void` `print();``};`` ` `Graph::Graph(``int` `V)``{``    ``this``->V = V;`` ` `    ``// Create a dynamic array of pointers``    ``adj = ``new` `bool``* [V];`` ` `    ``// Create a row for every pointer``    ``for` `(``int` `i=0; i

Output :

```0 1 1 0
0 0 1 0
1 0 0 1
0 0 0 1
```

A note on call of memset():
memset() is used separately for individual rows. We can’t replace these calls with one call because rows are allocated at different addresses and making a memset call like below would be disastrous.

```       // Wrong!! (Rows of matrix at different addresses)