Re: Pointers In VS2005

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



gnassar@xxxxxxxxx wrote:

I know I need to do:

XmlDocument^ J = new XmlDocument();
Thats fine.

XmlDocument^ J = gcnew XmlDocument;


but can I make a pointer to a managed resource anymore or
no?

So I guess can I do:
(XmlDocument^) *J;
J = 0;

You can not do it that way. A .NET handle is not a real memory address. In a managed environment the garbage collector is free to move objects around in the memory. There is a very real possibility that an object's address changes during the lifetime of the application, therefore you're not supposed to make pointers to them.

We all know that sometimes it's inevitable to use pointers, especially when a managed object must be passed to an unmanaged routine. For this reason it's possible to lock a managed handle for a brief period of time, using the pin_ptr syntax:

pin_ptr<MyClass> locked_class(class);

This ensures that the locked object is not moved around, nor is it garbage collected. You're supposed to release this lock as soon as possible. A pin_ptr can be treated as an unmanaged pointer, and passed to unmanaged code.

If you don't call unmanaged routines, you don't need to worry about pinning. Chances are that you can solve your problem with references. It is possible to create a reference to a handle:

XmlDocument ^ %

This is analogous to the native

NativeClass * &

syntax. For example:

void Function(XmlDocument^ % result)
{
result = gcnew XmlDocument;
}

So instead of getting a pointer to a handle, you grab a reference to it.

And finally, C++ (and I think C# too) has the concept of tracking pointer. It has the syntax of interior_ptr<T>. It was mainly invented to iterate through an array. For example:

array<int>^ buff = gcnew array<int>(100);
interior_ptr<int> p = & buff[0];
int count = buff->Length;
while(count--)
*p = 0;

I do image processing, and I found that the interior_ptr syntax generates much faster code than the array syntax (such as buff[index]).

interior_ptr doesn't lock the array. If the garbage collector has to move it around, it simply updates all interior_ptr instances for you automatically. That means interior_ptr probably has a slight overhead, and a restriction that it can't be a member of a class (you have to create it on the stack).

This is entirely different from unmanaged pointers. In a native application, you can not move an object in the memory, because there's no easy mechanism for updating every pointer referencing an object.

Tom
.



Relevant Pages

  • Re: Using ref
    ... "pass by reference" is based on syntax and not functionality (here I ... The C# code is clearly "by reference". ... pass the pointer). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question on LSP
    ... Sure, the developer must keep track of which type has been assigned to each pointer to manage complexity in creating the design, which is why the 'T' is in T*. ... Subtyping is a relation which does not ... Those object types are completely orthogonal to the semantics of being an object reference. ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Question on LSP
    ... Because construction paradigms have different goals. ... But that has nothing to do with what a pointer type ... But that does not mean that the semantics of an object reference is ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Modifying Class Object
    ... Before I start, note we're talking about semantics, not ... For example, as I used as reference in my first posting, the Java language spec. ... A pointer stores a reference to something. ...
    (comp.lang.python)
  • Re: Question on LSP
    ... equivalent addresses in the same context. ... Note that the language allows us to use a name like 'T' on the reference as a mnemonic so that the developer can keep track of what is happening with the indirection. ... Modern languages have proper pointer types. ...
    (comp.object)