Binding to C - Arrays and Access Violation

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 2 20:19:37 PST 2016


On Wednesday, 3 February 2016 at 00:37:25 UTC, Mike Parker wrote:
>
> The parameter to the C function should be declared as 
> extern(C), and so should your function implementation.
>
> extern(C) alias FuncPtr = double function(uint, const(double)*, 
> double*, void*);
> extern(C) void takeFuncPtr(FuncPtr);
>
> extern(C) double myfunc(uint n, const(double)* x, double* grad, 
> void* my_func_data) {
> ...
> }
>
> If you haven't done that, then this is quite possibly the root 
> of your problem.

Success! Couldn't have done it without your help.

I had originally had the equivalent of FuncPtr as extern(C), but 
I had removed that because myfunc wouldn't compile. I hadn't 
thought of putting those modifications on myfunc. Just assumed 
that I did the function pointers wrong.

A few extra questions: 1) In other parts of the code I'm using 
extern(System), but that doesn't work for these. Why is extern(C) 
used for function pointers?, 2) You use const(double)*, in other 
parts of the code I had converted the C code from const char* to 
const(char*). Does it matter where the pointer * falls?


More information about the Digitalmars-d-learn mailing list