cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: FFTW 3.3.5: Dynamic Arrays in C cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: cannam@127:
cannam@127:

cannam@127: Next: , Previous: , Up: Multi-dimensional Array Format   [Contents][Index]

cannam@127:
cannam@127:
cannam@127: cannam@127:

3.2.4 Dynamic Arrays in C

cannam@127: cannam@127:

We recommend allocating most arrays dynamically, with cannam@127: fftw_malloc. This isn’t too hard to do, although it is not as cannam@127: straightforward for multi-dimensional arrays as it is for cannam@127: one-dimensional arrays. cannam@127:

cannam@127:

Creating the array is simple: using a dynamic-allocation routine like cannam@127: fftw_malloc, allocate an array big enough to store N cannam@127: fftw_complex values (for a complex DFT), where N is the product cannam@127: of the sizes of the array dimensions (i.e. the total number of complex cannam@127: values in the array). For example, here is code to allocate a cannam@127: 5 × 12 × 27 rank-3 array: cannam@127: cannam@127:

cannam@127:
cannam@127:
fftw_complex *an_array;
cannam@127: an_array = (fftw_complex*) fftw_malloc(5*12*27 * sizeof(fftw_complex));
cannam@127: 
cannam@127: cannam@127:

Accessing the array elements, however, is more tricky—you can’t cannam@127: simply use multiple applications of the ‘[]’ operator like you cannam@127: could for fixed-size arrays. Instead, you have to explicitly compute cannam@127: the offset into the array using the formula given earlier for cannam@127: row-major arrays. For example, to reference the (i,j,k)-th cannam@127: element of the array allocated above, you would use the expression cannam@127: an_array[k + 27 * (j + 12 * i)]. cannam@127:

cannam@127:

This pain can be alleviated somewhat by defining appropriate macros, cannam@127: or, in C++, creating a class and overloading the ‘()’ operator. cannam@127: The recent C99 standard provides a way to reinterpret the dynamic cannam@127: array as a “variable-length” multi-dimensional array amenable to cannam@127: ‘[]’, but this feature is not yet widely supported by compilers. cannam@127: cannam@127: cannam@127:

cannam@127: cannam@127: cannam@127: cannam@127: cannam@127: