473,722 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "unrecogniz ed or unsupported array type":

array<float>^ vals = gcnew array<float>(di mension);
pin_ptr<float>s s=&vals[0];
unmanaged_funct ion(ss);

TIA
Oct 3 '06 #1
5 2392
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 "unrecogniz ed or unsupported array type":

array<float>^ vals = gcnew array<float>(di mension);
pin_ptr<float>s s=&vals[0];
unmanaged_funct ion(ss);

TIA
ops, substitute float* with float[] (a native array)
Oct 3 '06 #2
"xxx" <xx*@xxx.xxxwro te in message
news:45******** **************@ reader3.news.ti n.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 "unrecogniz ed or unsupported array type":

array<float>^ vals = gcnew array<float>(di mension);
pin_ptr<float>s s=&vals[0]; unmanaged_funct ion(ss);
A simple test to try to reproduce your problem works fine:

<code>
#define dimension 5

#pragma unmanaged

#include <stdio.h>

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

#pragma managed

int main()
{
array<float>^ vals = gcnew array<float>(di mension);
vals[0] = 0; vals[1] = 1; vals[2] = 2; vals[3] = 3; vals[4]=4;
pin_ptr<float>s s=&vals[0];
unmanaged_funct ion(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.xxxwro te in message
news:45******** **************@ reader3.news.ti n.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 "unrecogniz ed or unsupported array type":

array<float> ^ vals = gcnew array<float>(di mension);
pin_ptr<float> ss=&vals[0]; unmanaged_funct ion(ss);

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

<code>
#define dimension 5

#pragma unmanaged

#include <stdio.h>

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

#pragma managed

int main()
{
array<float>^ vals = gcnew array<float>(di mension);
vals[0] = 0; vals[1] = 1; vals[2] = 2; vals[3] = 3; vals[4]=4;
pin_ptr<float>s s=&vals[0];
unmanaged_funct ion(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
3276
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 in a strunct. There is a function in a DLL that takes this struct. This struct comes from a Visual Basic application. My code changes this string.
61
24491
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 'stretch'</td> <td valign="top" width="300">some data that won't 'stetch'</td> </tr> </table>
18
2562
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 load the vector, too. It just craps out when you return to the console, or in a Windows app, the message loop. I think it has something to do with the vector<float> going out of scope and trying to free memory, but don't know why it would do...
47
5081
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 are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
8
1568
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 array compared first and then each character compared with corresponding character. { const char msg = "msessage}", ch = "Za";
3
2211
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 { int intV; char charV;
2
2092
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 floats. I basically require the fastest way to convert a managed array of floats into an unmanaged array of floats. As far as the managed collection is implemented, ive read that the
1
9476
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 element, and the last element are known to be 0. I've implemented a -operator that returns a ComplexReference object that basically maps a complex<doubleinto the storage used in the array. What I would like to do is using the ComplexReference...
10
6465
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 MyObject<T: ChangeObject { ... public MyObject(string name, T value)
0
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.