help converting multi-dim arrays

David Harrison physics at utcs.UUCP
Fri Apr 5 20:45:17 AEST 1985

>From utfyzx!harrison Thu Apr  4 19:53:50 1985
Received: by utcs.UUCP (4.24/4.7) id AA12979; Thu, 4 Apr 85 19:53:46 est
Date: Thu, 4 Apr 85 19:53:46 est
From: utfyzx!harrison
Apparently-To: physics
The problem: a large piece of code with a number of arrays, some 
2 dimensional and a few 3 dimensional, accessed with lines like:
    for(row=0; row<numrows; row++)
        for(col=0; col<numcols; col++)
  [row][col] ...
The declaration of the matrices, such as
    static double data[MAXROWS][MAXCOLS]; /* just like FORTRAN */
is larger than any conceivable set of data it will be asked to 
deal with, and wastes scads of memory.  So, we want to convert 
the code to use malloc(3C) to dynamically allocate memory and 
save all that wasted space. But, we still want to access the 
matrices as:
 [row][col] ...
because: 1) we don't want to change all the code, and 
2)  the application makes one think in matrices and so this way 
of writing the code will be much easier to maintain.  Thus, we 
are trying to avoid:
    static double *data;
with access via stuff like:
            ... *(data + row*numcols + col) ...
Any ideas?
Dave Harrison, Dept. of Physics, Univ. of Toronto: ..utzoo!utcs!physics

More information about the Comp.lang.c mailing list