473,473 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointers In VS2005

I've been trying to learn dotnet myself though a bit of a basterdized
know-how of dotnet 2003 and previous straight unix C++ programming.
For some reason I have been able to get most of the mechanics down
quickly but there's something that's been eluding me.

I'm used to:

XmlDocument *xmld1p;
xmld1p = 0;

if (xmldp == 0){
....
}

This is all find and good (assuming I didn't make any minor spelling
mistakes here) in versions of VS prior to 2005. I understand that the
new operator is ^ for managed resources but can I make a pointer of an
object anymore?

I know I need to do:

XmlDocument^ J = new XmlDocument();
Thats fine. but can I make a pointer to a managed resource anymore or
no?

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

(That brings errors)
How would I do that?

Oct 10 '06 #1
2 1355
gn*****@gmail.com 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<MyClasslocked_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<intp = & 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
Oct 11 '06 #2
Tamas Demjen wrote:
array<int>^ buff = gcnew array<int>(100);
interior_ptr<intp = & buff[0];
int count = buff->Length;
while(count--)
*p = 0;
Oops, I meant

while(count--)
*p++ = 0;

And this is not necessarily the most efficient way to fill an array. I
just wanted to demonstrate the syntax.

Tom
Oct 11 '06 #3

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

Similar topics

5
by: GaryDean | last post by:
(my original post was inaccurate but this post accurately describes what I think is a very bad vs2005 bug) short description... Deleting a dataset and recreating it from the dataadapter causes...
10
by: musosdev | last post by:
Hi guys I'm trying to migrate to VS2005... I've managed to do that, but realised I'd opened my web projects as file projects, and I'm getting the error about network BIOS command limit. ...
6
by: Brad | last post by:
I have a win2003 server workstation with multiple webs, each web has it's own ip address. In VS2005, if I select to open an existing web site, select Local IIS, the dialog correctly displays a...
7
by: Frank Rizzo | last post by:
Is it me or is the speed of VS2005 actually slower than VS2003? The startup is pretty bad - even though I changed VS to display an empty environment. When I create a new form and want to change...
9
by: Rick | last post by:
Hi guys!! just one question, can i send pointers from VC++ 2005 to a vc++ 6.0 dll? if it is possible, how can i do this? does VS2005 have rules to send pointers? Regards.
9
by: yevvi | last post by:
Hi, We have a product with bunch of dlls which are now built with Visual Studio 2003. We want to switch the build to use VS2005. I have read that in VS2005 runtime libraries come as...
15
by: Joseph Geretz | last post by:
OK, I'll admit it up front - I just don't get it. Here's our previous VS2003 development model. Developers develop the WS solution on their own workstations, using their own IIS web servers...
4
by: chy1013m1 | last post by:
I am slightly confused as to how to reference multi-dimensional array with pointers. I've tried the following code, and I was able to reference 33 as pptr int multi = {{11,27,33}, {12,13,14}};...
1
by: Peter | last post by:
1) We create an assembly with Managed Extensions for C++ named some_name.dll. This assembly has source code like the following Byte * some_method() // a method returning a pointer, defined...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
1
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...
0
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
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
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...
0
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 ...
0
muto222
php
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.