What type of C # stands for C ++ float *?

I have a function that is called from a library in C ++ that has been imported into a C # project

I think it is requesting a pointer to an array. But I'm not sure how to make it work.

this is what it asks for function(float*, float*);

but if I do something like

 float[] f = {}; float[] f1 = {}; function(f,f1); 

he says there are invalid arguments.

+4
source share
5 answers

float * - type of float pointer in C #.

If a function expects pointer arguments to float (float *), then it is assumed that the function should work with pointers, possibly using pointer arithmetic. Therefore, it is important to keep this signature.

To pass float arrays in C # as floating point pointers (float *), you need to output / fix arrays in memory in an insecure context to get a pointer to them, which you can pass to the function to preserve its functionality:

 unsafe { fixed (float* ptr_f = f) //or equivalently "... = &f[0]" address of f[0] fixed (float* ptr_f2 = f2) //or equivalently "... = &f2[0]" address of f2[0] { function( ptr_f, ptr_f2 ); } } 

You will also need to mark your assembly as unsafe (in the tab "Project Properties"> tab "assembly"> allow unsafe code).

+2
source

If this is a method imported using DLLImport() , you can simply replace the array pointers with a typed array.

So signature:

 [DLLImport("some.dll")] SomeMethod(int* a, float* b) 

Is becoming

 [DLLImport("some.dll")] SomeMethod(int[] a, float[] b) 

Note that this assumes that the original c / C ++ method was expecting an array. This will not work if pointers are not intended to reference arrays.

+2
source

You need to know exactly what the C ++ function expects.

In C (and C ++), all of these function signatures are exactly the same for all goals and objectives:

 void foo( float *x , float *y ) ; void foo( float *x , float y[] ) ; void foo( float x[] , float *y ) ; void foo( float x[] , float y[] ) ; 

They all take 2 arguments, each of which contains a pointer to (containing the address) a variable of type float . And all these expressions are exactly identical in C / C ++:

 float x[] ; float *y ; float r1 = *(x+37) ; // all evaluate to the zero-relative float r2 = x[37] ; // 37th element of the array. float r3 = *(y+37) ; float r4 = y[37] ; 
  • The expression *x says: "Retrieve the float (4 bytes) located at the address contained in x .
  • The expression *(x+n) , where n is an integer value, says: "Take the address contained in x and add the offset in bytes obtained by the expression sizeof(float) * n . Extract the float value at the resulting address.
  • The array expression x[n] is exactly equivalent to the pointer expression *(x+n) .

And since arrays in C / C ++ do not have associated metadata describing their size, you need to know exactly what the function expects.

Typically, one passes a pointer (call by reference) to allow the caller to de-link to a point and set a value for you - the equivalent of the C # ref and out keywords:

 float x ; float y ; Foo( ref x , ref y ) ; // the callee MAY, but is not REQUIRED to set a value before returning to the caller. Bar( out x , out y ) ; // the callee MUST set a value before returning to the caller. 

The idiom that your function uses is always used to search for an array, although usually the size also goes through:

 void foo( float *array , int array_length ) ; 

Although this is not unusual, if the function expects the array to be a list of non-zero numeric values, it will be something like a C-style, NUL-terminated string. Given a function signature, for example:

 float compute_mean( float *Xs ) ; 

It is not unusual to expect it to be called like this:

 float Xs[] = { 3.0 , 2.5 , 9.8 , 7,5 , 0 , } ; float mean = compute_mean( Xs ) ; 

and the definition should look something like this:

 float compute_mean( float *Xs ) { float n = 0.0 ; float sum = 0.0 ; float mean ; while ( *p ) { ++n ; sum += *p++ ; } mean = n > 0 ? sum / n : 0.0 ; return mean ; } 

So you need to know the semantics of the method you are calling.

Hope this helps.

+1
source

which doesn't work at all in C #, when importing C / C ++ libraries into C # you should use IntPtr instead of float *, see here for some examples: float * from C to C #

0
source

It requests a float pointer, (the machine address of a point in memory that contains an IEEE floating point value.) C # does not allow controlled use of pointers, so you will need to build IntPtr and pass this to C ++ functions.

NOTE. Just because it is called IntPtr does not mean that it is just a pointer to integers. Int is because all pointers are integers. It can be a pointer to anything.

0
source

Source: https://habr.com/ru/post/1469070/


All Articles