LAPACK Archives

[Lapack] Discussing contiguity of C arrays

At it is stated that

# In C, however, a two-dimensional array is in row-major order.  Further, the
# rows of a two-dimensional C array need not be contiguous.  The array  #
#    double A[LDA][N];
# actually has LDA pointers to rows of length N.

This is not at all true, at least in ANSI C.  If LDA and N are
compile-time constants (as they must be in this syntax in ANSI C), the
resulting object is a continuous array of LDA*N doubles, in row-major
order of course, with the expression A[i][j] translated by the compiler
into something equivalent to (&A[0][0])[LDA*i+j].

It is of course a common approach in C to accommodate variable-size arrays
by declaring double **A=(double*)malloc(LDA*sizeof(double*)), and one has
an array of pointers that way.  One could even have double *A[LDA] if LDA
were fixed but N might vary, or double (*A)[N] for the opposite case.

Perhaps not many people are misled by the documentation there, but it
could be of some importance that C's static 2-dimensional arrays are
usable with LAPACK, even if they must be treated as transposed.

Thanks for the attention to this confusion,
Davis Herring
GRA, X-3

This product is sold by volume, not by mass.  If it appears too dense or
too sparse, it is because mass-energy conversion has occurred during

<Prev in Thread] Current Thread [Next in Thread>
  • [Lapack] Discussing contiguity of C arrays, Davis Herring <=

For additional information you may use the LAPACK/ScaLAPACK Forum.
Or one of the mailing lists, or