By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,465 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,465 IT Pros & Developers. It's quick & easy.

array<float>^ to float* conversion

P: n/a
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
Oct 3 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
xxx
xxx wrote:
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)
Oct 3 '06 #2

P: n/a
"xxx" <xx*@xxx.xxxwrote in message
news:45**********************@reader3.news.tin.it. ..
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[i]);
}

#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
Oct 3 '06 #3

P: n/a
xxx
Carl Daniel [VC++ MVP] wrote:
"xxx" <xx*@xxx.xxxwrote in message
news:45**********************@reader3.news.tin.it. ..
>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[i]);
}

#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)?
Oct 4 '06 #4

P: n/a
xxx wrote:
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
Oct 4 '06 #5

P: n/a
xxx
Carl Daniel [VC++ MVP] wrote:
xxx wrote:
>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 ;)
Oct 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.