d@0: d@0: d@0: Dynamic Arrays in C-The Wrong Way - FFTW 3.2.1 d@0: d@0: d@0: d@0: d@0: d@0: d@0: d@0: d@0: d@0: d@0: d@0: d@0:
d@0:

d@0: d@0: d@0: Previous: Dynamic Arrays in C, d@0: Up: Multi-dimensional Array Format d@0:


d@0:
d@0: d@0:

3.2.5 Dynamic Arrays in C—The Wrong Way

d@0: d@0:

A different method for allocating multi-dimensional arrays in C is d@0: often suggested that is incompatible with FFTW: using it will d@0: cause FFTW to die a painful death. We discuss the technique here, d@0: however, because it is so commonly known and used. This method is to d@0: create arrays of pointers of arrays of pointers of ...etcetera. d@0: For example, the analogue in this method to the example above is: d@0: d@0:

     int i,j;
d@0:      fftw_complex ***a_bad_array;  /* another way to make a 5x12x27 array */
d@0:      
d@0:      a_bad_array = (fftw_complex ***) malloc(5 * sizeof(fftw_complex **));
d@0:      for (i = 0; i < 5; ++i) {
d@0:           a_bad_array[i] =
d@0:              (fftw_complex **) malloc(12 * sizeof(fftw_complex *));
d@0:           for (j = 0; j < 12; ++j)
d@0:                a_bad_array[i][j] =
d@0:                      (fftw_complex *) malloc(27 * sizeof(fftw_complex));
d@0:      }
d@0: 
d@0:

As you can see, this sort of array is inconvenient to allocate (and d@0: deallocate). On the other hand, it has the advantage that the d@0: (i,j,k)-th element can be referenced simply by d@0: a_bad_array[i][j][k]. d@0: d@0:

If you like this technique and want to maximize convenience in accessing d@0: the array, but still want to pass the array to FFTW, you can use a d@0: hybrid method. Allocate the array as one contiguous block, but also d@0: declare an array of arrays of pointers that point to appropriate places d@0: in the block. That sort of trick is beyond the scope of this d@0: documentation; for more information on multi-dimensional arrays in C, d@0: see the comp.lang.c d@0: FAQ. d@0: d@0: d@0: d@0: