In the fftw3 documentation the standard example is:
#include <fftw3.h>
...
{
fftw_complex *in, *out;
fftw_plan p;
...
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
...
fftw_execute(p); /* repeat as needed */
...
fftw_destroy_plan(p);
fftw_free(in); fftw_free(out);
}
The following syntax also works:
#include <fftw3.h>
{
fftw_complex *in, *out;
fftw_plan p;
...
in = new fftw_complex[N];
out = new fftw_complex[N];
p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
...
fftw_execute(p); /* repeat as needed */
...
fftw_destroy_plan(p);
delete [] in;
delete [] out;
}
I would like to use smart pointers instead, perhaps something like:
#include <fftw3.h>
...
{
fftw_plan p;
...
auto *in = std::make_shared<fftw_complex[N]>();
auto *out = std::make_shared<fftw_complex[N]>();
p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
...
fftw_execute(p); /* repeat as needed */
...
}
But I can't seem to get the syntax worked out. Has anyone used smart pointers along with the FFTW3 library?
I would advise caution in using shared pointers in this context. Yes, you can give a raw pointer to the FFTW planning function (details below). However, this will not increment the reference counter to the shared pointer. This is problematic because the FFTW plan will know about the memory, but the shared pointer thinks it nobody needs the memory such that the deleter may be called. You'll get a segfault.
Shared pointers have no implicit conversion to the raw pointer, you'd want to use the '.get()' method. I.e.
p = fftw_plan_dft_1d(N, in.get(), out.get(), FFTW_FORWARD, FFTW_ESTIMATE);
But don't do this