Chris@19: Chris@19: Chris@19: Dynamic Arrays in C - FFTW 3.3.4 Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19: Chris@19:
Chris@19: Chris@19:

Chris@19: Next: , Chris@19: Previous: Fixed-size Arrays in C, Chris@19: Up: Multi-dimensional Array Format Chris@19:


Chris@19:
Chris@19: Chris@19:

3.2.4 Dynamic Arrays in C

Chris@19: Chris@19:

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

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

     fftw_complex *an_array;
Chris@19:      an_array = (fftw_complex*) fftw_malloc(5*12*27 * sizeof(fftw_complex));
Chris@19: 
Chris@19:

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

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