Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: FFTW 3.3.5: Dynamic Arrays in C Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42:
Chris@42:

Chris@42: Next: , Previous: , Up: Multi-dimensional Array Format   [Contents][Index]

Chris@42:
Chris@42:
Chris@42: Chris@42:

3.2.4 Dynamic Arrays in C

Chris@42: Chris@42:

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

Chris@42:

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

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

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

Chris@42:

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

Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: