Re: 2D arrays of variable size.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance





"Igor Tandetnik" <itandetnik@xxxxxxxx> wrote in message news:%23PJfKnz8JHA.4900@xxxxxxxxxxxxxxxxxxxxxxx

David Webber wrote:
I find myself needing 2D arrays of variable size for the first time
in C++.
...
Is there a better way?

Basically, no. This is precisely the calculation the compiler is doing when accessing 2d arrays (but the compiler needs to know N at compile time).

Thanks for the confirmation.

Rather than using macros, perhaps you should write a class to encapsulate access to your array. With some trickery, you could get this to compile:

Matrix m(30, 40);
m[1][2] = 42;

so that clients can use the familiar array access syntax.

You are telepathic :-)

Background:
In fact my clients are fluent in FORTRAN and not at all in C++. As a preliminary step, to extend their FORTRAN code (with global fixed-size arrays) to do what they want, and still remain readable to them, I have already changed the arrays (eg X) to

const int M=800;
const int N=800;

class CALCULATION
{

public:

static double array_X[M][N];

}

and used

#define X(i,j) (CALCULATION::array_X[i-1][j-1])

so that the guts of their FORTRAN code looks essentially the same in C++ :-)

They didn't really want it translating into C++; they just wanted extensions which were well-nigh impossible with the code as it was (even using FORTRAN 90 or 95). So as soon as we have tested the translation enough to make sure it's getting the same answers as the FORTRAN code (and at least as quickly), then I can start doing the clever bit, which should now be quite easy!

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm





.



Relevant Pages

  • Re: OO programming - illumination? - whoopsie
    ... > represented by a host of loosely related arrays. ... > compiler and the memory allocators. ... A pointer is usually a word. ... organs can be further modeled to include cells and so on and so forth. ...
    (comp.lang.java.programmer)
  • Re: OO programming - illumination? - whoopsie
    ... represented by a host of loosely related arrays. ... compiler and the memory allocators. ... As a matter of fact there is no such thing as a pointer. ... memory that just holds a terminator, which is 0 by convention. ...
    (comp.lang.java.programmer)
  • Re: Criticisms?
    ... justified today with machines 10 000 or more faster than a PDP-11. ... the feature were "built-in" to the language. ... but lean enough that the compiler can still do a good job. ... The interchangeability of arrays and pointers is pure genius (it maps very ...
    (comp.lang.c)
  • Re: Double precision arrays and subroutines
    ... arrays. ... What happens is that, in Fortran, these two program units are ... The subroutine and the program that calls it are compiled ... and normally the compiler doesn't know about one when it's ...
    (comp.lang.fortran)
  • Re: Can someone spot the error here?
    ... C, for example, has no strings, only character arrays. ... My programs generally do a lot of string handling, ... the compiler is strict) or an array of "MinChar .. ...
    (comp.lang.pascal.misc)