Multi-Dimension Array Question

  • Thread starter Adam Hartshorne
  • Start date
A

Adam Hartshorne

The input to a function of a 3rd party library I want to use requires a
double**, which is a multi-dimension array of doubles.

I have looked on the net etc and seen several ways of supposedly doing
this, but I don't seem to be able to get them to work. I was wondering
if anybody can tell me what I am doing wrong.

int rows = 10 ;
int cols = 10 ;

double** data ;

data = new (double*)[rows] ;

for (int i = 0 ; i < rows ; i++) {

data = new double[cols];

}

main.cpp(291) : error C2143: syntax error : missing ';' before '['
main.cpp(291) : error C2337: 'rows' : attribute not found; it is neither
a built-in nor a custom attribute that is accessible in the current
namespace

both the error point to the data = new (double*)[rows] line


May I don't need to create the multi-dimension array this way, I don't
know. As mentioned above, I have a 3rd party library function that
requests double** data.

One point is that I won't know the row and column sizes until runtime.

Any suggestions would be much appreciated,

Adam
 
B

ben

Looks like a Visual C++ bug. The code compiled fine with gcc. Try this:

typedef double* ptr_to_double;

int rows = 10;
int cols =10;

double** data = new ptr_to_double[rows];

//...

delete[] data;


ben
 
A

Adam Hartshorne

ben said:
Looks like a Visual C++ bug. The code compiled fine with gcc. Try this:

typedef double* ptr_to_double;

int rows = 10;
int cols =10;

double** data = new ptr_to_double[rows];

//...

delete[] data;


ben

The input to a function of a 3rd party library I want to use requires a
double**, which is a multi-dimension array of doubles.

I have looked on the net etc and seen several ways of supposedly doing
this, but I don't seem to be able to get them to work. I was wondering
if anybody can tell me what I am doing wrong.

int rows = 10 ;
int cols = 10 ;

double** data ;

data = new (double*)[rows] ;

for (int i = 0 ; i < rows ; i++) {

data = new double[cols];

}

main.cpp(291) : error C2143: syntax error : missing ';' before '['
main.cpp(291) : error C2337: 'rows' : attribute not found; it is neither
a built-in nor a custom attribute that is accessible in the current
namespace

both the error point to the data = new (double*)[rows] line


May I don't need to create the multi-dimension array this way, I don't
know. As mentioned above, I have a 3rd party library function that
requests double** data.

One point is that I won't know the row and column sizes until runtime.

Any suggestions would be much appreciated,

Adam



Thanks that did the trick! Any idea why that is the case?

Adam
 
V

Victor Bazarov

Adam said:
The input to a function of a 3rd party library I want to use requires
a double**, which is a multi-dimension array of doubles.

I have looked on the net etc and seen several ways of supposedly doing
this, but I don't seem to be able to get them to work. I was wondering
if anybody can tell me what I am doing wrong.

int rows = 10 ;
int cols = 10 ;

double** data ;

data = new (double*)[rows] ;

for (int i = 0 ; i < rows ; i++) {

data = new double[cols];

}

main.cpp(291) : error C2143: syntax error : missing ';' before '['


Drop the parentheses.

V
 
O

Old Wolf

Adam said:
The input to a function of a 3rd party library I want to use requires a
double**, which is a multi-dimension array of doubles.

Actually it is a pointer to pointer to double.
It can be confusing to call a pointer-to-pointer a
multi-dimension array. The term 'multi-dimension array' usually
suggests an array of arrays, eg:

double x[10][10];

Of course, this sort of array is not suitable for your function.
double** data ;
data = new (double*)[rows] ;

'new (double *)[rows]' means '(new (double *))[rows]'.

In other words, it allocates one pointer to double, and then
immediately dereferences a pointer to that (causing undefined
behaviour).

To allocate an array of pointers-to-double:

data = new double *[rows];
for (int i = 0 ; i < rows ; i++) {
data = new double[cols];
}

main.cpp(291) : error C2143: syntax error : missing ';' before '['


Good that your compiler won't compile such trash (it's certainly
an error), but bad because it means the compiler is not standards-
compliant :)
 
B

ben

A parser bug perhaps. VC++ might be a bit lazy and it expects

new XXX[nnn];

without parenthesis around XXX.

ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top