public static void Main()
{
string a ="abc";
string b=a;
a="xyz";
}
In this example, what is structure of memory, taken by these variable.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Sep 22, 2012, 8:21 AM
So, when 'a' is assigned a reference (effectively a pointer) to the string "xyz", it overwrites the reference which it previously contained to "abc".
Andy ManitaraPosted Sep 23, 2012, 5:39 AM
---------------------------------------
a -> #0x8f5c "abc"
a = "abc"
2. Memory address - Value
---------------------------------------
a,b -> #0x8f5c "abc"
a = b
3. Memory address - Value
---------------------------------------
b -> #0x8f5c "abc"
a -> #0x6f4e "xyz"
a = "xyz"
Something like that..
Shivanand ArurPosted Sep 23, 2012, 5:26 AM
When you write as a = "abc", then a pointer "a" is created on stack which points to the String Object created on heap (lets say the memory address is 100 ) which contains value "abc".
For example.... a (points to address) -------> 100(This address contains value abc)
When you write b = a, then another pointer "b" is created on the thread stack which also points to the same memory address (100) which contains value "abc".
For example....
a (points to address) -------> 100 and
b(also points to the same memory address) -------> 100
Now when you write a = "xyz", then the "a" pointer starts pointing to another memory area which is created in the heap (say the memory address is 101)
For example....
a (points to address) -------> 101( This address contains value xyz )
So now when you write Console.WriteLine(a); you will get the output as "xyz" instead of "abc".
Hope this helps you.
Please mark the Answer an ACCEPTED IF IT HELPS YOU.
Thank you.
Sharad GuptaPosted Sep 23, 2012, 5:15 AM
then what happen ......................
It(a) override the same memory area which is previously contained by abc or not..
Andy ManitaraPosted Sep 22, 2012, 1:46 PM
Shivanand ArurPosted Sep 22, 2012, 1:23 PM
Thank you.
Shivanand ArurPosted Sep 22, 2012, 7:15 AM
Sharad GuptaPosted Sep 22, 2012, 7:05 AM
VulpesPosted Sep 22, 2012, 7:01 AM
Sharad GuptaPosted Sep 22, 2012, 4:48 AM
Shivanand ArurPosted Sep 22, 2012, 4:03 AM