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