Solved

memory address of an object

Posted on 2012-04-04
5
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
ID: 37809639
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
ID: 37810273
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 75

Accepted Solution

by:
käµfm³d   👽 earned 100 total points
ID: 37810500
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
ID: 37811623
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
ID: 37814390
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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