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