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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Sep 22, 2012, 4:21 AM EDT
So, when 'a' is assigned a reference (effectively a pointer) to the string "xyz", it overwrites the reference which it previously contained to "abc".
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 22, 2012, 12:03 AM EDT
Sharad GuptaRank #111 · 20K reputationPosted Sep 22, 2012, 12:48 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 22, 2012, 3:01 AM EDT
Sharad GuptaRank #111 · 20K reputationPosted Sep 22, 2012, 3:05 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 22, 2012, 3:15 AM EDT
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 22, 2012, 9:23 AM EDT
Thank you.
Andy Manitara18 reputationPosted Sep 22, 2012, 9:46 AM EDT
Sharad GuptaRank #111 · 20K reputationPosted Sep 23, 2012, 1:15 AM EDT
then what happen ......................
It(a) override the same memory area which is previously contained by abc or not..
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 23, 2012, 1:26 AM EDT
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.
Andy Manitara18 reputationPosted Sep 23, 2012, 1:39 AM EDT
---------------------------------------
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..