I'm using VS C++.NET 2005 Express in /clr. How do I create, and then use, a
'^' pointer to a 'double'? That is, assuming its possible, please fill in
the question marks below (there are two of them):
double x = double(7) ;
double y ;
double^ x_ptr = ?x ;
y = ?x_ptr ; // y = double(7)
I'd know how to do this if 'x' and 'y' were an instances of a ref class, but
not when they are something like a double, int, long, etc. If I can remove
my current dependency on code of the form 'double *' (I need pointers to
doubles) I think I can go from /clr to /clr pure (or /clr safe, not sure
what the difference is between these last two, or which is 'stronger'). What
are the advantages of going 'purer' than /clr?
Thanks in advance for responses!
Carl Daniel [VC++ MVP] - 18 Nov 2005 04:29 GMT
> I'm using VS C++.NET 2005 Express in /clr. How do I create, and then
> use, a '^' pointer to a 'double'? That is, assuming its possible,
[quoted text clipped - 4 lines]
>
> double^ x_ptr = ?x ;
double^ x_ptr = x; // implicit boxing
> y = ?x_ptr ; // y = double(7)
y = (double)x_ptr; // no implicit unboxing
> I'd know how to do this if 'x' and 'y' were an instances of a ref
> class, but not when they are something like a double, int, long, etc.
[quoted text clipped - 3 lines]
> two, or which is 'stronger'). What are the advantages of going
> 'purer' than /clr?
/clr:safe is "stronger" than /clr:pure.
/clr - may generate mixed-mode code
/clr:pure - generates only IL
/clr:safe - generates only verifiable IL
-cd
Peter Oliphant - 18 Nov 2005 14:38 GMT
Thanks, Carl!
[==P==]
>> I'm using VS C++.NET 2005 Express in /clr. How do I create, and then
>> use, a '^' pointer to a 'double'? That is, assuming its possible,
[quoted text clipped - 26 lines]
>
> -cd
Holger Grund - 18 Nov 2005 15:41 GMT
> I'm using VS C++.NET 2005 Express in /clr. How do I create, and then use,
> a '^' pointer to a 'double'? That is, assuming its possible, please fill
[quoted text clipped - 4 lines]
>
> double^ x_ptr = ?x ;
Note that double^ is a reference type (it's what e.g. C# treats as
an object)
double x = 7.;
object o = (object)x; // boxing
If you want a managed pointer (this is what ref/out in C# yield, or
a float64& in CLR speak) you can use
// has C++ reference semantics x_ref = 1 assigns to x
double% x_ref = x;
// managed pointer, has C++ pointer semantics
interior_ptr<double> x_ptr = &x;
There is a performance overhead associated with boxing (essentially
it creates a full object - supporting several interfaces, strong object
identity etc. - around the data value)
Why the language designers chose to deviate from the semantics for
reference types is beyond me.
> y = ?x_ptr ; // y = double(7)
y =*x_ptr;
> I'd know how to do this if 'x' and 'y' were an instances of a ref class,
> but not when they are something like a double, int, long, etc. If I can
> remove my current dependency on code of the form 'double *' (I need
> pointers to doubles) I think I can go from /clr to /clr pure (or /clr
> safe, not sure what the difference is between these last two, or which is
> 'stronger'). What are the advantages of going 'purer' than /clr?
double* __gc corresponds with interior_ptr<double>
__box double* corresponds with double^
__box(x) is used to box a value. This conversion is implicit with C++/CLI,
but you probably want to be explicit and use a box(_cast) helper function
template.
You should be able to use /clr:pure with almost everything including
native pointers (double* in C++/CLI). /clr:pure can operate on "unmanaged
data" (i.e. memory that is not managed by the CLR execution engine).
MSIL can describe such operations. There is almost nothing you get by
going from /clr to /clr:pure except maybe making things much more simple
for a certain type of static analysis tools.
/clr:safe is much more strict and won't let you do anything with native
pointers
(at least almost). But many things involving managed pointers are not
verifiable
as well. Essentially, they're only useful for parameter passing (e.g. ref
and out
in C#). Managed pointers can only be used in contexts with automatic storage
duration.
hth
-hg