How to allocate an multi-dimensional array dynamically?

1
2
3
4
5
6
int get_no_var, get_no_eqn;
cin>>get_no_var;
cin>>get_no_eqn;

int * matrix;
matrix = new (nothrow) int [get_no_eqn][get_no_var];


Suggest a working code for this
Thanks also
I get the error:
Error 1 error C2540: non-constant expression as array bound

Error 2 error C2440: '=' : cannot convert from 'int (*)[1]' to 'int *'

In VS 2010 IDE
1
2
3
4
5
6
7
8
9
10
11
12
13
int get_no_var, get_no_eqn;
cin>>get_no_var;
cin>>get_no_eqn;

int **matrix = new int* [get_no_eqn]; // of course, it's a typo
for(ptrdiff_t i = 0; i < get_no_eqn; i++)
   matrix[i] = new int[get_no_var];

// ...

for(ptrdiff_t i = 0; i < get_no_eqn; i++)
   delete [] matrix[i];
delete [] matrix;
Last edited on
But I need to have a multidimensional matrix, or its complicated for me to code the program
Even if its not dynamic will do
Syufs matrix is two-dimensional.
Sorry, for the line
int **matrix = new int [get_no_eqn];

Im getting the error

Error 1 error C2440: 'initializing' : cannot convert from 'int *' to 'int **'

on VS Pro 2010
 
int (*matrix)[get_no_var] = new int[get_no_eqn][get_no_var];
Nope, gaorozcoo is wrong, Im getting 5 errors

Error 5 error C2036: 'int (*)[]' : unknown size
Error 1 error C2057: expected constant expression
Error 4 error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int (*)[]'
Error 2 error C2466: cannot allocate an array of constant size 0
Error 3 error C2540: non-constant expression as array bound
@SameerThigale

That line should prob be

int **matrix = new int* [get_no_eqn];

@gaorozcoo

Does the form you've used allow get_no_var to be non-const??
Thanks andywestken, you're code IS correct
Is this a part of your learning experience or its in a book. If a book, can you refer it to me?
My form works perfectly, here a working example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  
  int get_no_var, get_no_eqn;
  
  cout<<"Enter qty of Rows: ";
  cin>>get_no_eqn;
  
  cout<<"Enter qty of Columns: ";
  cin>>get_no_var;

  int (*matrix)[get_no_var] = new int[get_no_eqn][get_no_var];
  
  for(int i=0; i<get_no_eqn; i++)
        for(int j=0; j<get_no_var; j++)
               matrix[i][j]=8;
               
  for(int i=0; i<get_no_eqn; i++){
        for(int j=0; j<get_no_var; j++){
               cout<<matrix[i][j];
        }
        cout<<"\n";
  }

  system("PAUSE");
  
  delete [] matrix;
	
  return 0;
}
@andywestken

 
int **matrix;


Is a pointer to an array of pointer which is not the case here. What SameerThigale needs is a pointer to a 2D array and the way to do it is the one I posted.

I have tried the following (your form is used in line 18), it compiles but when you run it, the program just stop working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  
  int get_no_var, get_no_eqn;
  
  cout<<"Enter qty of Rows: ";
  cin>>get_no_eqn;
  
  cout<<"Enter qty of Columns: ";
  cin>>get_no_var;

  //int (*matrix)[get_no_var] = new int[get_no_eqn][get_no_var];//gaorozcoo form
  int **matrix = new int* [get_no_eqn];//andywestken form

  
  for(int i=0; i<get_no_eqn; i++)
        for(int j=0; j<get_no_var; j++)
               matrix[i][j]=8;
               
  for(int i=0; i<get_no_eqn; i++){
        for(int j=0; j<get_no_var; j++){
               cout<<matrix[i][j];
        }
        cout<<"\n";
  }

  system("PAUSE");
  
  delete [] matrix;
	
  return 0;
}
Last edited on
This is rejected:
1
2
int i=rand();
int (*a)[i] = new int[i][i];

test.cpp: In function 'int main()':
test.cpp:6:12: warning: ISO C++ forbids variable length array 'a'
test.cpp:6:27: error: 'i' cannot appear in a constant-expression
Note that the warning is for the i on the left of the assignment, and the error is for the i on the right. I thought it was weird that I had never encountered such a syntax before.

andywestken's array just needs to have its elements initialized.
1
2
3
int **matrix = new int*[get_no_eqn];
for (int a=0;a<get_no_eqn;a++)
    matrix[a]=new int[get_no_var];
(It wasn't andywestken's suggestion. Should have read the whole thread.)
Last edited on
@gaorozcoo

Out of interest, what compiler are you using?

I'm using Visual C++ 2008 and for this to compile

int (*matrix)[get_no_var] = new int[get_no_eqn][get_no_var];

get_no_var must be constant, which I understand to be ANSI C++ behaviour.

Andy

P.S. If I try to compile the above with non-const value for get_no_var I get the following errors for that line (int get_no_eqn = 3, int get_no_var = 4;)

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2540: non-constant expression as array bound
error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int (*)[]'
Last edited on
Just make a matrix class and save yourself the trouble. A multi-dimensional matrix is really a linear matrix in memory anyway... Then you can make it self-allocating, dynamic, out whatever else you need.
@helios and andywestken

I am using Dev-C++. I tried:

1
2
3
4
  get_no_var=rand();
  get_no_eqn=rand();

  int (*matrix)[get_no_var] = new int[get_no_eqn][get_no_var];


and it works. I am trying to find out if my form is ANSI C++ because I have been using it (in Dev-C++) for a while and I have not had problems so far.
Like Mathhead200, I would use an matrix/array class in my own code.

I'm only interested in the int (*matrix)[get_no_var] ... out of idle curiousity.
1
2
3
4
5
6
7
#include <vector>

int main(){
    std::vector< std::vector<int> >* matrix = new std::vector< std::vector<int> >;
    delete matrix;
    return 0;
}


Would this be OK?
Last edited on
gaorozcoo: Dev-C++ uses GCC as its compiler. GCC has for a long time allowed variable-sized stack arrays as a non-standard extension to the language. To disable extensions in GCC, use the switch --pedantic.
Last edited on
Topic archived. No new replies allowed.