473,513 Members | 7,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Value types vs unmanaged pointers in C++/CLI

This compiles:

value class SomeClass
{};
int main()
{
SomeClass obj;

SomeClass *p= &obj;
}
C:\c>cl /clr temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.41013
for Microsoft (R) .NET Framework version 2.00.41013.0
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
Microsoft (R) Incremental Linker Version 8.00.41013
Copyright (C) Microsoft Corporation. All rights reserved.

/out:temp.exe
temp.obj

C:\c>

Also this compiles:
value class SomeClass
{};
int main()
{
SomeClass obj;

pin_ptr<SomeClass> p= &obj;
}
C:\c>cl /clr temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.41013
for Microsoft (R) .NET Framework version 2.00.41013.0
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
Microsoft (R) Incremental Linker Version 8.00.41013
Copyright (C) Microsoft Corporation. All rights reserved.

/out:temp.exe
temp.obj

C:\c>

Can we get a regular pointer to a value object? Value types can not be
moved from the CLR?


--
Ioannis Vranos
Nov 17 '05 #1
1 1526
Ioannis,
This compiles:

value class SomeClass
{};
int main()
{
SomeClass obj;

SomeClass *p= &obj;
}

Can we get a regular pointer to a value object? Value types can not be
moved from the CLR?


This first case compiles because obj is on the local stack, and obviously
the stack won't be moved itself. Mind you, this is not verifiable, btw.

Had you tried this, however, you'd get an error:

ref struct Cont
{
SomeClass sc_;
};

int main()
{
Cont^ c = gcnew Cont;

SomeClass *p= &c->sc_;
}

t.cpp
t.cpp(17) : error C2440: 'initializing' : cannot convert from
'cli::interior_ptr
<Type>' to 'SomeClass *'
with
[
Type=SomeClass
]
Cannot convert a managed type to an unmanaged type

That said, I did not see anything in the C++/CLI that very obviously
expresses these restrictions; pretty much all coverage of pointers and
interior pointers talks only about "objects in the GC heap", but not about
values on the stack.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1869
by: Rennie deGraaf | last post by:
Is it better/more efficient to throw exceptions by value, as in void func() throw (SomeException) { // do some stuff throw SomeException("error message"); } void bar() {
5
1540
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
5
2077
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
1
1401
by: Fedor Buyakov | last post by:
hello all i have a specific question according hiding unmanaged (native) types in metadata for assemblies generated by c++ with managed extensions. i understand what these types are nedeed in...
6
1290
by: marek | last post by:
Hello All, we are doing a quite a big project that contains at the lowest level an unmenaged c++ classes. Above it there are managed wrappers and at the top there are ASP.NET pages. Can anyone...
1
2366
by: quat | last post by:
I have two unmanaged pointer in a managed form class: IDirect3D9* d3dObject; IDirect3DDevice9* d3dDevice; In a member function of the form, I call: d3dObject->CreateDevice(...
23
2159
by: Tomás | last post by:
Anything wrong with the following code?: #include <cstdlib> int main() { for (unsigned i = 0; i != 1000; ++i) { int *p = reinterpret_cast<int*>( std::rand() );
14
5775
by: zon7 | last post by:
Hi, what i'm trying to do is obtaining a value from a function in a ref class. In this context I can't use the "ref" and "out" operators. I have tried all i can imagine but still hasn't it...
10
13609
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
0
7254
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,...
0
7153
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
7373
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
7432
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...
0
7519
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...
0
5677
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,...
1
5079
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...
0
1585
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 ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.