Seg fault when calling C code

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 15 14:11:54 PDT 2014


On 05/15/2014 01:55 PM, Andrew Brown wrote:
 > extern(C) {
 >    void regress(int nInd, int nCov, ref double[] x, ref double[] y, ref
 > double[] rOut);
 > }

I don't think that should even be allowed. C functions should not know 
or be compatible with 'ref' D parameters. Define the arguments as simple 
'double *' or 'const double *'.

That makes sense because your C function is defined that way anyway.

 > void main(){
 >    int nInd = 5;
 >    int nCov = 3;
 >    double[] x = new double[nCov * nInd];
// ...
 >    regress(5, 3, x, y, residuals);

You want to pass the address of the first array member: x.ptr (&(x[0]) 
would work as well). (Same for y.)

Otherwise, what ends up happening is that the address of the x and y 
slices are passed and C has no idea of what that is.

Ali



More information about the Digitalmars-d-learn mailing list