when defined(windows):
const LibraryName = "fftw3.dll"
elif defined(macosx):
const LibraryName = "libfftw3(|.0).dylib"
else:
const LibraryName = "libfftw3.so"
type fftw_complex* = array[2, cdouble]
proc fftw_alloc_complex*(n: csize): ptr fftw_complex {.cdecl,
importc: "fftw_alloc_complex", dynlib: LibraryName.}
When I try to allocate an array with the library and then fill it, Nim crashes. The simplest example I can give is
import fftw3
type UncheckedArray{.unchecked.}[A] = array[1, A]
proc `$`(a: fftw_complex): string = $(a[0]) & " + " & $(a[1]) & "i"
when isMainModule:
var a = fftw_alloc_complex(10)
var b = cast[UncheckedArray[fftw_complex]](a)
for i in 0 .. < 10:
b[i] = [1.0, i.float64]
for i in 0 .. < 10:
echo b[i]
Here I use UncheckedArray because the library returns a pointer to the beginning of the actual array, and I may not know the size statically, so I make it into an unchecked array in order to iterate on it.
With this definition, the code crashes at runtime with SIGSEGV: Illegal storage access. (Attempt to read from nil?).
Now, doing any of the following will make everything work fine:
Unfortunately, in my actual example I cannot do any of these: the size and values are not known until runtime, and the explicit type annotation seems not to work in my actual use case.
Is anyone able to figure out what is happening? I am sorry I am not able to give a self-contained example: this is the most minimal use case I could produce