array<float>^ to float* conversion

X

xxx

Hi all, i'm new in visual c++ and i'm having troubles converting types.
Let me explain: i have an unmanaged c++ function that wants an float*
parameter but i have an array<float>^, how i can covert it?
the following code doesn't show up any error during compile time but
crashes at runtime telling "unrecognized or unsupported array type":

array<float>^ vals = gcnew array<float>(dimension);
pin_ptr<float>ss=&vals[0];
unmanaged_function(ss);

TIA
 
X

xxx

xxx said:
Hi all, i'm new in visual c++ and i'm having troubles converting types.
Let me explain: i have an unmanaged c++ function that wants an float*
parameter but i have an array<float>^, how i can covert it?
the following code doesn't show up any error during compile time but
crashes at runtime telling "unrecognized or unsupported array type":

array<float>^ vals = gcnew array<float>(dimension);
pin_ptr<float>ss=&vals[0];
unmanaged_function(ss);

TIA

ops, substitute float* with float[] (a native array)
 
C

Carl Daniel [VC++ MVP]

xxx said:
Hi all, i'm new in visual c++ and i'm having troubles converting types.
Let me explain: i have an unmanaged c++ function that wants an float*
parameter but i have an array<float>^, how i can covert it?
the following code doesn't show up any error during compile time but
crashes at runtime telling "unrecognized or unsupported array type":

array<float>^ vals = gcnew array<float>(dimension);
pin_ptr<float>ss=&vals[0]; unmanaged_function(ss);

A simple test to try to reproduce your problem works fine:

<code>
#define dimension 5

#pragma unmanaged

#include <stdio.h>

void unmanaged_function(float vals[])
{
for (int i = 0; i < dimension; ++i)
printf("%f\n",vals);
}

#pragma managed

int main()
{
array<float>^ vals = gcnew array<float>(dimension);
vals[0] = 0; vals[1] = 1; vals[2] = 2; vals[3] = 3; vals[4]=4;
pin_ptr<float>ss=&vals[0];
unmanaged_function(ss);
}
</code>

Please provide more information about exactly what you're trying to do and
exactly how it's failing (and how you're determining that it's failing).

-cd
 
X

xxx

Carl said:
xxx said:
Hi all, i'm new in visual c++ and i'm having troubles converting types.
Let me explain: i have an unmanaged c++ function that wants an float*
parameter but i have an array<float>^, how i can covert it?
the following code doesn't show up any error during compile time but
crashes at runtime telling "unrecognized or unsupported array type":

array<float>^ vals = gcnew array<float>(dimension);
pin_ptr<float>ss=&vals[0]; unmanaged_function(ss);

A simple test to try to reproduce your problem works fine:

<code>
#define dimension 5

#pragma unmanaged

#include <stdio.h>

void unmanaged_function(float vals[])
{
for (int i = 0; i < dimension; ++i)
printf("%f\n",vals);
}

#pragma managed

int main()
{
array<float>^ vals = gcnew array<float>(dimension);
vals[0] = 0; vals[1] = 1; vals[2] = 2; vals[3] = 3; vals[4]=4;
pin_ptr<float>ss=&vals[0];
unmanaged_function(ss);
}
</code>

Please provide more information about exactly what you're trying to do and
exactly how it's failing (and how you're determining that it's failing).

-cd


thanks for your answer. It work also with my custom classes so i'm
wondering why vc++ doesn't cast in a simple manner (eg. (float[] ) vals)?
 
C

Carl Daniel [VC++ MVP]

xxx said:
thanks for your answer. It work also with my custom classes so i'm
wondering why vc++ doesn't cast in a simple manner (eg. (float[] )
vals)?

Ah, so the problem was something that you didn't mention in your first post
at all:

what cant array<float>^ be converted to float[] via a C-style cast?

Simple answer: because the language designers didn't choose to make that
legal. In the end, that's a good thing, as type coersions like this should
be easy to find in the source code and easy to recognize. In a case like
this, conversion from the managed array<float>^ to the native float[] (which
is synonymous with float*) requires a pin operation on the GC heap. That's
not something that you want to have happening willy-nilly. Rather, it
should be something rare and easy to find by examining the source code -
hence, pin_ptr<T>.

-cd
 
X

xxx

Carl said:
xxx said:
thanks for your answer. It work also with my custom classes so i'm
wondering why vc++ doesn't cast in a simple manner (eg. (float[] )
vals)?

Ah, so the problem was something that you didn't mention in your first post
at all:

what cant array<float>^ be converted to float[] via a C-style cast?

Simple answer: because the language designers didn't choose to make that
legal. In the end, that's a good thing, as type coersions like this should
be easy to find in the source code and easy to recognize. In a case like
this, conversion from the managed array<float>^ to the native float[] (which
is synonymous with float*) requires a pin operation on the GC heap. That's
not something that you want to have happening willy-nilly. Rather, it
should be something rare and easy to find by examining the source code -
hence, pin_ptr<T>.

-cd
ok, now it's more clear. thanks again ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top