Multi-Dimension Array Question

Discussion in 'C++' started by Adam Hartshorne, Jun 8, 2005.

  1. 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
     
    Adam Hartshorne, Jun 8, 2005
    #1
    1. Advertising

  2. Adam Hartshorne

    ben Guest

    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" <> wrote in message
    news:d85l3j$b5r$...
    > 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
     
    ben, Jun 8, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    > news:d85l3j$b5r$...
    >
    >>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
     
    Adam Hartshorne, Jun 8, 2005
    #3
  4. 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 = new double[cols];
    >
    > }
    >
    > main.cpp(291) : error C2143: syntax error : missing ';' before '['


    Drop the parentheses.

    V
     
    Victor Bazarov, Jun 8, 2005
    #4
  5. Adam Hartshorne

    Old Wolf Guest

    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 = 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 :)
     
    Old Wolf, Jun 8, 2005
    #5
  6. Adam Hartshorne

    ben Guest

    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
     
    ben, Jun 8, 2005
    #6
  7. Adam Hartshorne

    ben Guest

    Yeah that's a better solution!

    ben

    >
    > Drop the parentheses.
    >
    > V
    >
    >
     
    ben, Jun 8, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Makiyo
    Replies:
    3
    Views:
    535
    Richard Heathfield
    Feb 22, 2004
  2. Eric Laberge

    Multi-dimension array assignments

    Eric Laberge, Aug 22, 2005, in forum: C Programming
    Replies:
    3
    Views:
    463
    Dave Thompson
    Aug 27, 2005
  3. Replies:
    11
    Views:
    527
    Alexei A. Frounze
    Sep 29, 2005
  4. Replies:
    12
    Views:
    590
    Thad Smith
    Oct 10, 2005
  5. Luuk
    Replies:
    15
    Views:
    832
    Nobody
    Feb 11, 2010
Loading...

Share This Page