I
Ivan K.
I am looking at some legacy code, which begins by
allocating a double matrix with the dmatrix()
function from NRC as follows:
double **A, **augin, **augout, **aa;
A = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
/* ... */
The dmatrix() function is appended below.
At some point in the program, these lines occur.
/* adjust indices rather than reallocate */
for(i=2;i<=nnonm;i++) A = A[i-1] + total + addcol;
for(i=2;i<=nnonm;i++) aa = aa[i-1] + total + addcol;
for(i=2;i<=nnonm;i++) augin = augin[i-1] + total + addcol + 1 +
nsims;
for(i=2;i<=nnonm;i++) augout = augout[i-1] + total + addcol + 1
+ nsims;
and it looks to me that what is going on here is that as A, aa,
augin, and augout are pointers to double dimensioned arrays, then
A is a pointer to a single array and the statement
"A[i-1] + total + addcol" is adding an integer to
a pointer to an array.
Is this correct? If so, what is happening
when one adds an integer to a pointer to an array?
Thank you for your help;
Ivan;
double **dmatrix( long nrl, long nrh, long ncl, long nch)
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
*/
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
double **m;
/* allocate pointers to rows */
m=(double **) malloc((unsigned int)((nrow
+NR_END)*sizeof(double*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof
(double)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
allocating a double matrix with the dmatrix()
function from NRC as follows:
double **A, **augin, **augout, **aa;
A = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3);
augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
/* ... */
The dmatrix() function is appended below.
At some point in the program, these lines occur.
/* adjust indices rather than reallocate */
for(i=2;i<=nnonm;i++) A = A[i-1] + total + addcol;
for(i=2;i<=nnonm;i++) aa = aa[i-1] + total + addcol;
for(i=2;i<=nnonm;i++) augin = augin[i-1] + total + addcol + 1 +
nsims;
for(i=2;i<=nnonm;i++) augout = augout[i-1] + total + addcol + 1
+ nsims;
and it looks to me that what is going on here is that as A, aa,
augin, and augout are pointers to double dimensioned arrays, then
A is a pointer to a single array and the statement
"A[i-1] + total + addcol" is adding an integer to
a pointer to an array.
Is this correct? If so, what is happening
when one adds an integer to a pointer to an array?
Thank you for your help;
Ivan;
double **dmatrix( long nrl, long nrh, long ncl, long nch)
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch]
*/
{
long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
double **m;
/* allocate pointers to rows */
m=(double **) malloc((unsigned int)((nrow
+NR_END)*sizeof(double*)));
if (!m) nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof
(double)));
if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for(i=nrl+1;i<=nrh;i++) m=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}