473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

array<float>^ to float* conversion

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
5 2367
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yelve Yakut | last post by:
Hello, I am currently messing around with some sample code I found It is about linking VB and CPP through DLL's http://www.flipcode.com/articles/article_vbdlls.shtml There is a string embaded...
61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
18
by: Active8 | last post by:
I put the bare essentials in a console app. http://home.earthlink.net/~mcolasono/tmp/degub.zip Opening output.fft and loading it into a vector<float> screws up, but input1.dat doesn't. It does...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
8
by: Greenhorn | last post by:
Hi, Those relational operators have operands of all numerical type int,char,float etc. They also are working for character arrays. Whats the logic behind their working. Is the length of the...
3
by: Dean L. Howen | last post by:
Hi friends, In C++, we can declare a struct and write it into file just by giving the address the strct instance (using &). for example: ///////////////////////////////// typedef struct Test...
2
by: Daniel Mori | last post by:
Hi, Im hoping someone can give me some advice. Im doing some development using VS Whidbey 2005 beta 1. Im about to implement some highly time critical code related to a managed collection of...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.