Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: Chris@42: FFTW 3.3.5: Dynamic Arrays in C-The Wrong Way 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: Previous: , Up: Multi-dimensional Array Format   [Contents][Index]

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

3.2.5 Dynamic Arrays in C—The Wrong Way

Chris@42: Chris@42:

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

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

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

Chris@42:

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

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