1

I have float array inside C++ function.

C++ function

void bleplugin_GetGolfResult(float* result)
{
    float *array = new float[20];
    for(int i=0; i < 20; i++)
      array[i]= 25;
    result = array;
    //DEBUG PRINTING1
    for(int i=0; i < 20; i++)
       cout << result[i] << endl;//Here is correct
    return;
}

Inside C#

[DllImport ("__Internal")]
private static unsafe extern void bleplugin_GetGolfResult (float* result);

public static unsafe float[] result = new float[20];

public unsafe static void GetGolfREsult(){
    fixed (float* ptr_result = result) //or equivalently "... = &f2[0]" address of f2[0]
    {           
        bleplugin_GetGolfResult( ptr_result );
        //DEBUG PRINTING2
        for(int i = 0; i < 20; i++)
            Debug.Log("result data " + ptr_result[i]);
    }
    return;
}

I called GetGolfREsult() from another function to get result.

//DEBUG PRINTING1 has correct output.

But //DEBUG PRINTING2 produced 0 only.

What could be wrong?

4
  • Where are you assigning values to the array in your C# method? Commented Dec 6, 2017 at 14:39
  • Never use the assignment operator on the parameter Commented Dec 6, 2017 at 14:43
  • 3
    result is passed by value. Therefore it is pointless assigning to it. Your problems begin way before there is any C# code. Your C++ function is currently of no use whatsoever. You are getting ahead of yourself. Make sure your C++ code works before you try to call it from C++. And there's no need for unsafe code here. Commented Dec 6, 2017 at 14:52
  • If you can add a hint about the array size (and fix your C++ code as David pointed out), you can use the solution provided in this answer. Commented Dec 6, 2017 at 14:52

3 Answers 3

4

As UnholySheep and nvoigt already stated,

result = array;

overrides the address of the passed pointer, making you lose the reference to the calling function.

Directly writing to your parameter should solve this.

result[i] = 25;

Further you dont actually have to use pointers in c#. You can actually do the following:

Declare your Import like this:

private static extern void bleplugin_GetGolfResult (float arr[]);

Then you can call it like this:

float arr = new float[20];
bleplugin_GetGolfResult(arr);
Sign up to request clarification or add additional context in comments.

Comments

3

This line in your C++ code:

float *array = new float[20];

creates a new array, which you operate on in C++. Then control returns to C#, who has it's own array and that's still unchanged. Why don't you write to the array you got?

4 Comments

I think the actual flaw in OP's thinking is that result = array; would assign (copy?) this array back to C#
Because I like to copy array data to result. What you meant to write the array i got. I don't get it.
Actually I used float *array as a simple example. That array is already there for some reason and I can't use parameter result to create array at C++ function.
@batuman result = array; does not modify the passed in array (it wouldn't do that if you were working exclusively in C++ either), it just redirects the local pointer to the array. You need to write directly into the passed in array (by result[i] = 25; instead of array[i] = 25;)
0

The problem is that you use the assignment operator on the parameter result which prevents the data from being transferred to the C# array on return.

Using the following C++ example:

void z(int * x)
{
  x = new int(4);
}

int main()
{
  int * x = new int(-2);
  z(x);
  cout<<*x<<endl;
}

The output for this is -2 not 4 because you use the assignment operator on the parameter.

2 Comments

What should I do for that?
@batuman could pass an array to the function with a length of 20 then you would just change the value of each spot in the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.