d@0: d@0:
d@0:d@0: d@0: Next: Dynamic Arrays in C-The Wrong Way, d@0: Previous: Fixed-size Arrays in C, d@0: Up: Multi-dimensional Array Format d@0:
We recommend allocating most arrays dynamically, with
d@0: fftw_malloc
. This isn't too hard to do, although it is not as
d@0: straightforward for multi-dimensional arrays as it is for
d@0: one-dimensional arrays.
d@0:
d@0:
Creating the array is simple: using a dynamic-allocation routine like
d@0: fftw_malloc
, allocate an array big enough to store N
d@0: fftw_complex
values (for a complex DFT), where N is the product
d@0: of the sizes of the array dimensions (i.e. the total number of complex
d@0: values in the array). For example, here is code to allocate a
d@0: 5 × 12 × 27 rank-3 array:
d@0:
d@0:
fftw_complex *an_array; d@0: an_array = (fftw_complex*) fftw_malloc(5*12*27 * sizeof(fftw_complex)); d@0:d@0:
Accessing the array elements, however, is more tricky—you can't
d@0: simply use multiple applications of the `[]' operator like you
d@0: could for fixed-size arrays. Instead, you have to explicitly compute
d@0: the offset into the array using the formula given earlier for
d@0: row-major arrays. For example, to reference the (i,j,k)-th
d@0: element of the array allocated above, you would use the expression
d@0: an_array[k + 27 * (j + 12 * i)]
.
d@0:
d@0:
This pain can be alleviated somewhat by defining appropriate macros, d@0: or, in C++, creating a class and overloading the `()' operator. d@0: The recent C99 standard provides a way to reinterpret the dynamic d@0: array as a “variable-length” multi-dimensional array amenable to d@0: `[]', but this feature is not yet widely supported by compilers. d@0: d@0: d@0: d@0: d@0: