Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multi-Dimension Array Question

Reply
Thread Tools

Multi-Dimension Array Question

 
 
Adam Hartshorne
Guest
Posts: n/a
 
      06-08-2005
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[i] = 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
 
Reply With Quote
 
 
 
 
ben
Guest
Posts: n/a
 
      06-08-2005
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

"Adam Hartshorne" <(E-Mail Removed)> wrote in message
news:d85l3j$b5r$(E-Mail Removed)...
> 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[i] = 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



 
Reply With Quote
 
 
 
 
Adam Hartshorne
Guest
Posts: n/a
 
      06-08-2005
ben wrote:

> 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
>
> "Adam Hartshorne" <(E-Mail Removed)> wrote in message
> news:d85l3j$b5r$(E-Mail Removed)...
>
>>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[i] = 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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-08-2005
Adam Hartshorne wrote:
> 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[i] = new double[cols];
>
> }
>
> main.cpp(291) : error C2143: syntax error : missing ';' before '['


Drop the parentheses.

V


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      06-08-2005
Adam Hartshorne wrote:
> 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[i] = 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

 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      06-08-2005
A parser bug perhaps. VC++ might be a bit lazy and it expects

new XXX[nnn];

without parenthesis around XXX.

ben

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



 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      06-08-2005
Yeah that's a better solution!

ben

>
> Drop the parentheses.
>
> V
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Question concerning array.array and C++ Fabio Python 0 11-05-2008 03:53 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
"array of Derived" is not a kind-of "array of Base" question Joseph Turian C++ 11 01-19-2005 11:53 AM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments