Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 27-02-2008, 03:02 PM   #1 (permalink)
Broken In
 
Join Date: Sep 2004
Location: Mysore
Posts: 137
Default C# code


int iNum = 16; // declaring a value type int
object oVal1 = iNum; // boxing the value type int
object oVal2; // creating a new object reference on the stack
oVal2 = oVal1; // assigning the object reference to another reference
oVal1 = 32; // changing the value of the boxed reference
System.Console.Writeln(oVal1);
System.Console.Writeln(oVal2); //printing the values


Now my ques is

Why does changing the value of the reference of one object does not change the value of the other object as both are references pointing to the same location?

I know that changing the value of a boxed reference does not change its value in the value type.. but when reference typed values are changed should they be reflected in other references also?
hariharan is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 28-02-2008, 07:52 PM   #2 (permalink)
Broken In
 
Join Date: Nov 2005
Location: kolkata
Posts: 135
Default Re: C# code

oVal1 = 32;
in this line a new object is created and assigned to oVal1. oVal2 still pointing to the old object so these two values are different.
tanmoy_rajguru2005 is offline  
Old 28-02-2008, 09:00 PM   #3 (permalink)
Alpha Geek
 
Krazy_About_Technology's Avatar
 
Join Date: Jun 2004
Location: Noida - India
Posts: 765
Default Re: C# code

Its simple. Everytime you box a value to an object, a new memory location is allocated for it. So for your example, something like this is going on:

Code:
int iNum = 16;          // iNum @ location XX ->value 16
object oVal1 = iNum;    // oVal1 @ location YY ->value 16
object oVal2;          // oVal2 @ location ZZ ->value null
oVal2 = oVal1;         // oVal2 @ location YY ->value 16
oVal1 = 32;           // oVal1 @ location AA -> value 32

System.Console.Writeln(oVal1);   // Prints oVal1@location AA=32
System.Console.Writeln(oVal2);  // Prints oVal2@location YY=16
Here XX, YY, ZZ and AA are assumed memory locations of these variables.

I hope this had clear things up for you. Correct me if i am wrong somewhere.

__________________
Dell Inspiron 1525 - C2D 2 Ghz, 3GB, 250GB, X3100 :)

Samsung Omnia Pro B7610 with Stock WM 6.1 ROM

Blog: http://www.sumitbhardwaj.co.in/blog
Krazy_About_Technology is offline  
Old 29-02-2008, 04:05 PM   #4 (permalink)
Broken In
 
Join Date: Sep 2004
Location: Mysore
Posts: 137
Default Re: C# code

thanks 4 u time.. both of u guys..

Quote:
Originally Posted by tanmoy_rajguru2005 View Post
oVal1 = 32;
in this line a new object is created and assigned to oVal1. oVal2 still pointing to the old object so these two values are different.
this new object for oVal1 is created only in the case of boxing. isn't it?


Quote:
Here XX, YY, ZZ and AA are assumed memory locations of these variables.
Is it possible to see the memory location actually assigned?

Are the memory locations different for the objects every time they are assigned a value?

Or is it the case the memory locations are different for the objects only when they are assigned a value which needs to be boxed.
hariharan is offline  
Old 29-02-2008, 06:04 PM   #5 (permalink)
Alpha Geek
 
Krazy_About_Technology's Avatar
 
Join Date: Jun 2004
Location: Noida - India
Posts: 765
Default Re: C# code

Well you cant see the actual memory location even if you mark this code as unsafe. That is because object is a managed data type, and you cant get address of an managed data type. If it would be an int or something then after putting this code in an unsafe{ } block you can take address of a variable same as you do it in C ie. by using & operator.

No, when an regular object is assigned to another object, it always points to the second objects memory address. This is evident from these lines:

Code:
object oVal1 = iNum;    // oVal1 @ location YY ->value 16
object oVal2;          // oVal2 @ location ZZ ->value null
oVal2 = oVal1;         // oVal2 @ location YY ->value 16
First oVal2 was at ZZ, then after doing oVal2=oVal1, oVal2 is at YY, the location of oVal1. This is normal behavior of objects.

But any statement that causes boxing, a new memory is given to object. So when oVal2=32 was executed, a value type (int) is assigned to a reference type (object) so oVal2 is given a new memory.

Hope this clear things up for you
__________________
Dell Inspiron 1525 - C2D 2 Ghz, 3GB, 250GB, X3100 :)

Samsung Omnia Pro B7610 with Stock WM 6.1 ROM

Blog: http://www.sumitbhardwaj.co.in/blog
Krazy_About_Technology is offline  
Old 29-02-2008, 07:52 PM   #6 (permalink)
Broken In
 
Join Date: Sep 2004
Location: Mysore
Posts: 137
Default Re: C# code

yeah perfectly clear now.. thanks a lot 4 ur time..
hariharan is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
lock code..not security code magneticme200 Mobiles and Tablets 3 29-12-2007 07:15 PM
Help with Code Mr.Cool QnA (read only) 10 09-12-2006 02:37 PM
Bar Code desertwind QnA (read only) 4 23-07-2005 02:02 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:52 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2