0

I want to convert my code from C++ to C#. The C++ contains pointer, and I want to convert it to C#.

Here is the C++ code:

void callkernel(int *obsx,int *obsy,int *obsz,int no,int nx,int *x,
    int *y,int *z,int dx,int dy,int dz,double **k)
{
double dxd2 = dx/2;
double dyd2 = dy/2;
double dzd2 = dz/2;

for (int q=0; q < nx;q++)
{
    double xb = x[q]-dxd2; double xa = x[q]+dxd2;
    double yb = y[q]-dyd2; double ya = y[q]+dyd2;
    double zb = z[q]-dzd2; double za = z[q]+dzd2;
    for (int m = 0; m<no; m++)
    {
        double kernel = mk(obsx[m],obsy[m],obsz[m],xb,yb,zb,xa,ya,za);
        k[m][q] = kernel;
    }
}
}

Then It will called by:

callkernel(obsx,obsy,obsz,no,nx,x,y,z,dx,dy,dz,k);

here is my code in C#:

void callkernel(int[] obsx, int[] obsy, int[] obsz, int no, int nx, int[] x,
    int[] y, int[] z, int dx, int dy, int dz, ref double[][] k)
    {
        double dxd2 = dx / 2;
        double dyd2 = dy / 2;
        double dzd2 = dz / 2;

        for (int q = 0; q < nx; q++)
        {
            double xb = x[q] - dxd2; double xa = x[q] + dxd2;
            double yb = y[q] - dyd2; double ya = y[q] + dyd2;
            double zb = z[q] - dzd2; double za = z[q] + dzd2;
            for (int m = 0; m < no; m++)
            {
                double kernel = mk(obsx[m], obsy[m], obsz[m], xb, yb, zb, xa, ya, za);
                k[m][q] = kernel;
            }
        }
    }

It called by:

callkernel(obsx, obsy, obsz, no, nx, x, y, z, dx, dy, dz, ref k);

They are small part of a big code in my program. With the code that I did, it seems have different result. Can anyone let me know where is my mistake?

ehmind
  • 265
  • 3
  • 10
  • 2
    You really should use more descriptive names for your variables, this is very hard to read – Tim Feb 20 '14 at 07:52
  • You have a method (?) in there named `mk`. We need to see the code for that as well – flipchart Feb 20 '14 at 07:54
  • Just a tip (doesn't solve your problem, but makes the code nicer to use): you don't need to pass `k` as `ref`. Arrays contents are passed by reference by default. See http://stackoverflow.com/a/967413/789683 for more info – flipchart Feb 20 '14 at 07:57
  • Could you tell us the example results from c++ code and the results from c# one? My first guess is the `mk` function which would make the difference. @flipchart I think the ref is used with apropriate siginificance as it enforces developer to assign value to the `k`, seens reasonable with that amount of parameters :-) – XAMlMAX Feb 20 '14 at 07:58
  • @flipchart yes I have a method mk. the mk code is too long to write here. But basically it will generate a single number. The code still have another teens method to get one final result number. It will be hard to compare the input and output of the program if we just see the above code. I just want to know the basic concept to converting pointer in C++ to C#. – ehmind Feb 20 '14 at 08:33
  • 1
    `mk` is likely the problem. The `ref` before `double[][] k` is pointless but the code is functionally identical. This means that the only place the problem could be in `mk`. I would check that `mk` is providing the results you expect by writing unit tests for it. Your basic understanding of converting pointers to C# code is correct. – Jonathan Dickinson Feb 20 '14 at 08:43
  • I already got the answer. It causes from another method. The conversion works well. Btw, thank you for your answer. It complements my understanding of pointers. I appreciate your time. – ehmind Feb 20 '14 at 09:09

1 Answers1

0

I already got the answer. It causes from another method. The conversion works well. Btw, thank you for your answer. It complements my understanding of pointers. I appreciate your time.

Ps: I want to vote the answer but idk how to do it.

ehmind
  • 265
  • 3
  • 10