473,698 Members | 1,888 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 2391
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
3274
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
24477
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
2557
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
5070
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
1563
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
2208
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
2087
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
9450
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
6462
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
8600
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,...
0
9155
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8890
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
8858
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...
1
6517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
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
3
1997
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.