Re: 2D arrays of variable size.
- From: "David Webber" <dave@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 22 Jun 2009 14:24:30 +0100
"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
.
- Follow-Ups:
- Re: 2D arrays of variable size.
- From: Giovanni Dicanio
- Re: 2D arrays of variable size.
- From: Igor Tandetnik
- Re: 2D arrays of variable size.
- References:
- 2D arrays of variable size.
- From: David Webber
- Re: 2D arrays of variable size.
- From: Igor Tandetnik
- 2D arrays of variable size.
- Prev by Date: Re: 2D arrays of variable size.
- Next by Date: Re: Power Point 2007 Automation with Visual C++ - missing methods in "Presentations" header
- Previous by thread: Re: 2D arrays of variable size.
- Next by thread: Re: 2D arrays of variable size.
- Index(es):
Relevant Pages
|