473,414 Members | 1,697 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,414 software developers and data experts.

How are "const" literal Strings stored?

Back in the old Visual C++ days, it was always important to define constant
strings only once so that they wouldn't be stored over and over again in
memory. Has .NET resolved this issue? Consider these two code snippets:

Example A
========
const String hey="hey";
MessageBox.Show(hey);
MessageBox.Show(hey);
MessageBox.Show(hey);

Example B
========
MessageBox.Show("hey");
MessageBox.Show("hey");
MessageBox.Show("hey");

Does Example A make more efficient use of memory since "hey" is only stored
once in memory?... or is Example B just as efficient because the compiler is
smart enough to realize that "hey" is the *same* constant literal string
that is referenced three times in a row?

Sincerely,

Michael Jackson's Nose
Jul 21 '05 #1
5 2124
Lecture,

The latter is true. The compiler is smart enough to know that "hey" is
the same literal string, and will store it in the assembly once, referencing
it once (at least, it should).

The runtime is even smart enough to know that if you have "hey" in one
assembly (as a literal) and "hey" in another assembly, only one instance
will be created per app domain.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Lecture Snoddddgrass" <ma***@hamburg.fry> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
Back in the old Visual C++ days, it was always important to define constant strings only once so that they wouldn't be stored over and over again in
memory. Has .NET resolved this issue? Consider these two code snippets:

Example A
========
const String hey="hey";
MessageBox.Show(hey);
MessageBox.Show(hey);
MessageBox.Show(hey);

Example B
========
MessageBox.Show("hey");
MessageBox.Show("hey");
MessageBox.Show("hey");

Does Example A make more efficient use of memory since "hey" is only stored once in memory?... or is Example B just as efficient because the compiler is smart enough to realize that "hey" is the *same* constant literal string
that is referenced three times in a row?

Sincerely,

Michael Jackson's Nose

Jul 21 '05 #2
Lecture Snoddddgrass <ma***@hamburg.fry> wrote:
Back in the old Visual C++ days, it was always important to define constant
strings only once so that they wouldn't be stored over and over again in
memory. Has .NET resolved this issue? Consider these two code snippets:

Example A
========
const String hey="hey";
MessageBox.Show(hey);
MessageBox.Show(hey);
MessageBox.Show(hey);

Example B
========
MessageBox.Show("hey");
MessageBox.Show("hey");
MessageBox.Show("hey");

Does Example A make more efficient use of memory since "hey" is only stored
once in memory?... or is Example B just as efficient because the compiler is
smart enough to realize that "hey" is the *same* constant literal string
that is referenced three times in a row?


Example B is just as efficient - string literals are interned.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Is it not also true that if you do somethign like:

a = "hello";
b = "hello";

That the "hello" will only be stored once overall? When, later in the
program you do a

b += "world";

Only then does the "hello" string get copied somewhere else as it is now a
different string from the original?

I believe I read that somewhere but do not remember for a fact where.

--
gabriel
Jul 21 '05 #4
Strings are pointers to a string table somewhere in memory. In your first
example a and b both point to the same string table entry so "hello" is stored
once. If you concat "world" to the string, you create a brand new string, in
this
case "helloworld" that is then placed in said string table. a still points to
"hello"
while b points to this new string.

This points out why string concatenation is bad, at least at run-time where the
compiler can't optimize it out, because you can wind up with *intermediate*
strings
in the string table that you never wind up using.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"gabriel" <no@no--spam.com> wrote in message
news:6d**************************@msgid.meganewsse rvers.com...
Is it not also true that if you do somethign like:

a = "hello";
b = "hello";

That the "hello" will only be stored once overall? When, later in the
program you do a

b += "world";

Only then does the "hello" string get copied somewhere else as it is now a
different string from the original?

I believe I read that somewhere but do not remember for a fact where.

--
gabriel

Jul 21 '05 #5
100
Hi Lecture,

Both are equivalent. They produce the same code. The only difference is that
when you declare a string as a constant you make the code easier to maintain
and can export those strings for using by other assemblies. However when the
compiler compiles the code it reads the string literal from the assembly
metadata and uses the string as if it was written as a string in the place
of using.

So,
const String hey="hey";
MessageBox.Show(hey);
and
MessageBox.Show("hey");

are exactly the same

HTH
B\rgds
100

"Lecture Snoddddgrass" <ma***@hamburg.fry> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
Back in the old Visual C++ days, it was always important to define constant strings only once so that they wouldn't be stored over and over again in
memory. Has .NET resolved this issue? Consider these two code snippets:

Example A
========
const String hey="hey";
MessageBox.Show(hey);
MessageBox.Show(hey);
MessageBox.Show(hey);

Example B
========
MessageBox.Show("hey");
MessageBox.Show("hey");
MessageBox.Show("hey");

Does Example A make more efficient use of memory since "hey" is only stored once in memory?... or is Example B just as efficient because the compiler is smart enough to realize that "hey" is the *same* constant literal string
that is referenced three times in a row?

Sincerely,

Michael Jackson's Nose

Jul 21 '05 #6

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

Similar topics

23
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
5
by: Lecture Snoddddgrass | last post by:
Back in the old Visual C++ days, it was always important to define constant strings only once so that they wouldn't be stored over and over again in memory. Has .NET resolved this issue? Consider...
7
by: xgngli | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of...
16
by: Chris | last post by:
Looking at some code I see a declaration inside a function like static const string s("some string"); Does the static serve any purpose here?
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.