Callum Linington
Callum Linington

Reputation: 14417

C++ and C# Array and Void Conversion

I'm converting C++ code to C# code, it happens to be the Fast Fourier Transform on an image in the Frequency Domain. Just to give some context.

Here is a link to the C++ Code : fft.cc

I have this function called Step, its signature is this in C++:

void step ( int n, int mj, float a[], float b[], float c[], float d[], float w[], float sgn );

and is called like this:

step ( n, mj, &x[0*2+0], &x[(n/2)*2+0], &y[0*2+0], &y[mj*2+0], w, sgn );

I want to convert it to C#, now this function operates on either Y or X arrays depending on whether its a Forward Fast Fourier Transform or a Backward Fast Fourier Transform. (More context)

What I don't understand, is that in C#, doing x[0*2+0] does nothing.... firstly putting an integer in the square brackets actually calls for a variable at a position in that array.

But what is it doing in C++, I know that the & is equiv to the ref C# keyword, it is saying get the contents from the array that is being pointed too, but I feel that in this context it means more than just that.

So how would you call that function in C#, obviously this fails:

step(n, mj, x[0 * 2 + 0], x[(n / 2) * 2 + 0], y[0 * 2 + 0], y[mj * 2 + 0], w, sgn);

Upvotes: 0

Views: 1092

Answers (3)

Wilbert
Wilbert

Reputation: 7399

The C++ version lets you pass in pointers (think references) to certain elements in the data.

For a C# version, I would suggest you pass in the two arrays directly, and then only pass in the indices, so

 void step ( int n, int mj, float a[], float b[], float c[], float d[], 
      float w[], float sgn );

becomes

void Step(float[] x, float[] y, int n, int mj, int ai, int bi, int ci, int di,
     int w, float sign)
{ 
      float a = x[ai];
      float b = x[bi];
      float y = y[ci];

      // .... compute whatever


     x[ai] = a;
     x[bi] = b;
     //...
}

And if you encounter something like a[5] in the C++ function, you can simply use

float aa = x[ai+5];

but of course you need to think about writing the new value back, if necessary.

Upvotes: 0

111WARLOCK111
111WARLOCK111

Reputation: 867

Remove addresses, no way of changing a value except making it a class with functions to change the values or returning them. instead of float a[] use float[] a

Here's how your function looks like, no headers needed:

public class entity
{
    public void Step(int n, int mj, float a[], float[] b, float[] c, float[] d, float[] w, float sgn)
    {
       ...
    }

    Step(0, 0, new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 1f, 1f }, new[] { 1f, 1f }, 1f);
}

Upvotes: 0

PMF
PMF

Reputation: 17185

In C++, this is passing the address of an entry of the array to the function. This doesn't work in C#, because you cannot take the address of an entry of an array. I suggest rewriting the declaration to pass the indices only and pass the array separately. Or make the x and y array a member of the containing class.

Uh, or if you're only interested in the single element, you could pass the element instead. Remember that float b[] in C++ is the same as float* b, so if skip only accesses b[0] (and not [b+1]), then that would probably be the easier solution).

When we have a C++ declaration like

void step (float a[]) 

which is the same as

void step (float* a)

And we call it with

step(&x[2]);

The function step sees an array of float that starts at the second entry of x. So, inside step

float f = a[0];

would refer to the element at x[2];

Of course, one could also address a[27], but that's prone for errors, since we do not know the (remaining) length of x.

To help finding the optimal solution in this case, one needs to know what step does with the arguments. Would it be possible to post the step function or parts of it?

Upvotes: 2

Related Questions