cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: FFTW 3.3.8: Dynamic Arrays in C-The Wrong Way cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: cannam@167:
cannam@167:

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

cannam@167:
cannam@167:
cannam@167: cannam@167:

3.2.5 Dynamic Arrays in C—The Wrong Way

cannam@167: cannam@167:

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

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

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

cannam@167:

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

cannam@167: cannam@167: cannam@167: cannam@167: cannam@167: