Solved

memory address of an object

Posted on 2012-04-04
5
398 Views
Last Modified: 2012-08-13
In Java or possibly in C#, objectName.toString() gives the memory address of objectName.

How this is done in vb.net?

The reason behind my question is to find out when an object is being instantiated using different constructors in the following fashion in fact only one object is created or two?

Public Class Car
.
.
    Sub New(ByVal gaslevel As Single, ByVal MPG As Single)
        Me.mGasLevel = gaslevel
        Me.mMPG = MPG
        'MessageBox.Show("memory location by New(2 pram): " & Me.ToString())
    End Sub

    Sub New(ByVal gaslevel As Single, ByVal MPG As Single, ByVal mileage As Single)
        Me.New(gaslevel, MPG)
        Me.mMileage = mileage
        MessageBox.Show("memory location by New(3 pram): " & Me.ToString(`)
    End Sub
.
.
End Class
-------------------------------
Case 1: When New(ByVal gaslevel As Single, ByVal MPG As Single) is called one object is created. No discussion on this.

Case 2: When New(ByVal gaslevel As Single, ByVal MPG As Single, ByVal mileage As Single) is called, if we get:

a) memory location by New(2 pram): d100
    memory location by New(3 pram): d100

One object is created.

b) memory location by New(2 pram): d100
    memory location by New(3 pram): d200

Then, two objects are created. d100 is lost (no longer referenced by the object variable). But d200 is referenced by object variable.

Any thoughts on this?

Thank you.
0
Comment
Question by:Mike Eghtebas
5 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
Comment Utility
Various points.
I doubt the ToString gives the location of memory of the object - at best it gives the location of the string.
.Net has dynamic memory management.  That means you might actually get an adress and a few seconds later the object still exist but at a DIFFERENT memory adress.

Sub New(ByVal gaslevel As Single, ByVal MPG As Single)
        Me.mGasLevel = gaslevel
        Me.mMPG = MPG
        'MessageBox.Show("memory location by New(2 pram): " & Me.ToString())
    End Sub

That fills in one object.

    Sub New(ByVal gaslevel As Single, ByVal MPG As Single, ByVal mileage As Single)
        Me.New(gaslevel, MPG)
        Me.mMileage = mileage
        MessageBox.Show("memory location by New(3 pram): " & Me.ToString(`)
    End Sub
That as the first line CREATES a second object and fills in the existing object - hence two objects
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 100 total points
Comment Utility
ToString() is a method on the object class which gives the string representation of an object (its full type by default).

You can override it to return your own string representation. For example, DateTime structure overrides this method to allow you to get datetime is a specific format by supplying the format string.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 100 total points
Comment Utility
I cannot find any explicit mention of this, but going by the information I found in the language specification regarding constructors (§9.3.1 Instance Constructors) I would say that there are not multiple objects created in memory. The reason I say this is that when you invoke a constructor, whether you explicitly add it or not, at some point a MyBase.New is called. Your memory allocation (I believe) is happening within a base constructor*; everything else is just field initialization. I would expect that under the hood, there is some bit of information carried along to the top-most constructor which alerts it as to how much memory to allocate. Remember that fields/properties, to a computer, are just offsets from some base pointer in memory. As long as the constructor receives back from the base constructor the starting address of the object from within memory, it can set the properties and variables accordingly because it just goes to offset X from that memory address received from the base constructor.

* When I say "memory allocation ... is happening within a base constructor" I am not referring to the immediate base; rather I am referring to (for lack of specific information) Object.New, since everything in .NET derives from Object. Per the spec, every constructor must invoke another constructor, but since Object is the super class of everything in .NET, I take that to be the last stop for constructors (though perhaps there is some underlying native call going on when you get to that level). It is my guess that the constructor for Object is the constructor which actually allocates memory--every other constructor only initializes members.
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 100 total points
Comment Utility
In C# this is done through Pointer Expressions, which are "unsafe code" and require a special compiler setting.  VB.NET does not support Pointer Expressions.

How to: Obtain the Address of a Variable (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/zcbcf4ta%28v=vs.100%29.aspx

Unsafe Code and Pointers (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx

I hope this helps.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
Comment Utility
The only .NET language that completely supports unmanaged code (pointers) is C++.

As it has been said, but in other words, VB is managed code only, so it does not have access to pointers, as most modern programming languages, because pointers were one of the word causes of security problems in the past. Also in other words, C# can be compiled as unmanaged code, having access to pointers, but in a limited way, and that limits a lot what you can do in the application. I would be surprised that Java could do it, because I have always known Java as C++ without pointers.

You won't get 2 objects, but only one. That is the main reason why Me.New should be the first line in an alternate constructor. The job of the 2 methods in combined into only one allocation.

You can see that by using a tool such as the CLRProfiler, available somewhere on the Microsoft site.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now