473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointer / reference clarification

I need some clarification with the whole "no pointers in VB.NET" thing,
because it seems like there are "quazi-pointers" happening but I want to
make sure I'm interpreting all this correctly.

Here's a generic class declaration for the sake of example:

Public clsClass
'properties, methods
End Class

And now some declarative snippits, followed by abstracts of what I think is
happening:

Dim objClass As clsClass

That sets up something like this in memory:

{objClass = Nothing} <- Can hold a memory address, but currently holds
Nothing so far

So as I understand it, this essentially creates something akin to a
"pointer". It cannot be treated like a C++ pointer, but it is something of
a pointer in the sense that it will "point" to memory.

objClass = New clsClass

Now it's set to an instance of an object, and so here's what I assume is now
going on in memory:

{@ address 1, clsClass instance} <- is now physically stored in the managed
memory area
{objClass = @1} <- now stores the memory address for lookup of the clsClass
instance

Dim objClass2 As clsClass = objClass

Because they are references, this is what I assume is happening after this
declaration:

{@ address 1, clsClass instance} <- still physically stored in the managed
memory area
{objClass = @1} <- still storing the memory address for lookup
{objClass2 = @1} <- now storing the same memory address for lookup as
objClass

Is this essentially correct? I still have only one actual instance, and two
smaller pointers (well, references) available for lookups against the same
instance. Let's add one more:

Dim objClass3 as New clsClass

{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}

So now two instances, three references (two of which point to the same
instance). Now I start wandering out into stuff I'm not sure I understand,
for example a HashTable. Here's my guess:

Dim objHash as New HashTable:

{@ memory address 1, clsClass instance}
{objClass = 1}
{objClass2 = 1}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{objHash = 3}

And then...

objHash.Add("A", objClass2)

{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{HashTable "A" = @1} <- ...something like that?
{objHash = @3}

Is the Hash table storing data in this fashion? Do I still have just one
instance of clsClass?

And finally does the garbage collector kill the instance for me once all the
references are gone?

objClass = Nothing
objClass2 = Nothing

{@ memory address 1, clsClass instance}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{HashTable "A" = 1}
{objHash = 3}

Is the HashTable item "A" maintaining my instance @1?

objHash.Remove(objHash("A"))

[...GC runs]

{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{objHash = @3}

I'm starting a project using lots and lots of classes in multiple 2D arrays
of hashtables and I want to make sure I have this stuff more or less correct
in my mind before I start allocating memory like gangbusters.
Nov 21 '05 #1
2 1110
Workgroups,

It looks to me like you are interpreting this correctly.

Kerry Moorman
"Workgroups" wrote:
I need some clarification with the whole "no pointers in VB.NET" thing,
because it seems like there are "quazi-pointers" happening but I want to
make sure I'm interpreting all this correctly.

Here's a generic class declaration for the sake of example:

Public clsClass
'properties, methods
End Class

And now some declarative snippits, followed by abstracts of what I think is
happening:

Dim objClass As clsClass

That sets up something like this in memory:

{objClass = Nothing} <- Can hold a memory address, but currently holds
Nothing so far

So as I understand it, this essentially creates something akin to a
"pointer". It cannot be treated like a C++ pointer, but it is something of
a pointer in the sense that it will "point" to memory.

objClass = New clsClass

Now it's set to an instance of an object, and so here's what I assume is now
going on in memory:

{@ address 1, clsClass instance} <- is now physically stored in the managed
memory area
{objClass = @1} <- now stores the memory address for lookup of the clsClass
instance

Dim objClass2 As clsClass = objClass

Because they are references, this is what I assume is happening after this
declaration:

{@ address 1, clsClass instance} <- still physically stored in the managed
memory area
{objClass = @1} <- still storing the memory address for lookup
{objClass2 = @1} <- now storing the same memory address for lookup as
objClass

Is this essentially correct? I still have only one actual instance, and two
smaller pointers (well, references) available for lookups against the same
instance. Let's add one more:

Dim objClass3 as New clsClass

{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}

So now two instances, three references (two of which point to the same
instance). Now I start wandering out into stuff I'm not sure I understand,
for example a HashTable. Here's my guess:

Dim objHash as New HashTable:

{@ memory address 1, clsClass instance}
{objClass = 1}
{objClass2 = 1}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{objHash = 3}

And then...

objHash.Add("A", objClass2)

{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{HashTable "A" = @1} <- ...something like that?
{objHash = @3}

Is the Hash table storing data in this fashion? Do I still have just one
instance of clsClass?

And finally does the garbage collector kill the instance for me once all the
references are gone?

objClass = Nothing
objClass2 = Nothing

{@ memory address 1, clsClass instance}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{HashTable "A" = 1}
{objHash = 3}

Is the HashTable item "A" maintaining my instance @1?

objHash.Remove(objHash("A"))

[...GC runs]

{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{objHash = @3}

I'm starting a project using lots and lots of classes in multiple 2D arrays
of hashtables and I want to make sure I have this stuff more or less correct
in my mind before I start allocating memory like gangbusters.

Nov 21 '05 #2
Sounds about right. In VB, we deal in "Value types" and "Reference types".
Value types are the basic types that hold their data within their own memory
allocation. Numeric types, booleans, chars, dates are all value types.
Reference types contain a pointer to another memory location that holds the
data. Arrays, strings, class types, etc. are all reference types. Here's
MSKB discussion on this:
http://msdn.microsoft.com/library/de...ueRefTypes.asp

"Workgroups" <no*****@domainless.com> wrote in message
news:Us********************@speakeasy.net...
I need some clarification with the whole "no pointers in VB.NET" thing,
because it seems like there are "quazi-pointers" happening but I want to
make sure I'm interpreting all this correctly.

Here's a generic class declaration for the sake of example:

Public clsClass
'properties, methods
End Class

And now some declarative snippits, followed by abstracts of what I think
is happening:

Dim objClass As clsClass

That sets up something like this in memory:

{objClass = Nothing} <- Can hold a memory address, but currently holds
Nothing so far

So as I understand it, this essentially creates something akin to a
"pointer". It cannot be treated like a C++ pointer, but it is something
of a pointer in the sense that it will "point" to memory.

objClass = New clsClass

Now it's set to an instance of an object, and so here's what I assume is
now going on in memory:

{@ address 1, clsClass instance} <- is now physically stored in the
managed memory area
{objClass = @1} <- now stores the memory address for lookup of the
clsClass instance

Dim objClass2 As clsClass = objClass

Because they are references, this is what I assume is happening after this
declaration:

{@ address 1, clsClass instance} <- still physically stored in the managed
memory area
{objClass = @1} <- still storing the memory address for lookup
{objClass2 = @1} <- now storing the same memory address for lookup as
objClass

Is this essentially correct? I still have only one actual instance, and
two smaller pointers (well, references) available for lookups against the
same instance. Let's add one more:

Dim objClass3 as New clsClass

{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}

So now two instances, three references (two of which point to the same
instance). Now I start wandering out into stuff I'm not sure I
understand, for example a HashTable. Here's my guess:

Dim objHash as New HashTable:

{@ memory address 1, clsClass instance}
{objClass = 1}
{objClass2 = 1}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{objHash = 3}

And then...

objHash.Add("A", objClass2)

{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{HashTable "A" = @1} <- ...something like that?
{objHash = @3}

Is the Hash table storing data in this fashion? Do I still have just one
instance of clsClass?

And finally does the garbage collector kill the instance for me once all
the references are gone?

objClass = Nothing
objClass2 = Nothing

{@ memory address 1, clsClass instance}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{HashTable "A" = 1}
{objHash = 3}

Is the HashTable item "A" maintaining my instance @1?

objHash.Remove(objHash("A"))

[...GC runs]

{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{objHash = @3}

I'm starting a project using lots and lots of classes in multiple 2D
arrays of hashtables and I want to make sure I have this stuff more or
less correct in my mind before I start allocating memory like gangbusters.

Nov 21 '05 #3

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

Similar topics

15
by: Torsten Mohr | last post by:
Hi, i'd like to pass a reference or a pointer to an object to a function. The function should then change the object and the changes should be visible in the calling function. In perl this...
3
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
5
by: George2 | last post by:
Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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
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...

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.