Copyright (C) 2000-2012 |
GNU Info (fftw.info)Dynamic Arrays in CDynamic Arrays in C ------------------- Often, especially for large arrays, it is desirable to allocate the arrays dynamically, at runtime. This isn't too hard to do, although it is not as straightforward for multi-dimensional arrays as it is for one-dimensional arrays. Creating the array is simple: using a dynamic-allocation routine like `malloc', allocate an array big enough to store N `fftw_complex' values, where N is the product of the sizes of the array dimensions (i.e. the total number of complex values in the array). For example, here is code to allocate a 5x12x27 rank 3 array: fftw_complex *an_array; an_array = (fftw_complex *) malloc(5 * 12 * 27 * sizeof(fftw_complex)); Accessing the array elements, however, is more tricky--you can't simply use multiple applications of the `[]' operator like you could for static arrays. Instead, you have to explicitly compute the offset into the array using the formula given earlier for row-major arrays. For example, to reference the (i,j,k)-th element of the array allocated above, you would use the expression `an_array[k + 27 * (j + 12 * i)]'. This pain can be alleviated somewhat by defining appropriate macros, or, in C++, creating a class and overloading the `()' operator. automatically generated by info2www version 1.2.2.9 |