473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

string test

Hi, All
According to C#.Net

string s = "foo";
string z = "foo";

s and z gonna share the same memory location, correct me if I am wrong. If
it is correct, how can I prove this. Is there any tool can show me the
memory location.

Thanks in advance!
Nov 17 '05 #1
2 1264
Green wrote:
string s = "foo";
string z = "foo";

s and z gonna share the same memory location, correct me if I am wrong. If
it is correct, how can I prove this. Is there any tool can show me the
memory location.


1) Why would you wan't to prove this?

2) object.ReferenceEquals(s,z)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #2

"Green" <ma*********@yahoo.com> wrote in message
news:Oo**************@TK2MSFTNGP09.phx.gbl...
Hi, All
According to C#.Net

string s = "foo";
string z = "foo";

s and z gonna share the same memory location, correct me if I am wrong. If
it is correct, how can I prove this. Is there any tool can show me the
memory location.

Thanks in advance!


s and z are different memory locations, they are the variables
(register/local allocated in this case) that contain the value of the
reference pointing to the GC heap allocated object of type string.

You can't take the contents of the variable, but you can take the address of
the first character of the string, so if you run following, you'll see that
both adresses are equal. When you change the contents of one of the strings,
you'll notice the addresses are different.

string a = "foo";
string b = "foo";

unsafe {
fixed (char* aa = a)
{
Console.WriteLine("{0:x}", (int)new IntPtr(aa));
}

fixed (char* aa = b)
{
Console.WriteLine("{0:x}", (int)new IntPtr(aa));
}
}

Willy.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
4
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings...
35
by: Cor | last post by:
Hallo, I have promised Jay B yesterday to do some tests. The subject was a string evaluation that Jon had send in. Jay B was in doubt what was better because there was a discussion in the C#...
2
by: Tony Johansson | last post by:
Hello! string object are immutable but if I have this kind of construction se below 1. will this be done so only one string object is created or will a new string object be created to concat...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.