473,320 Members | 2,054 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,320 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
Nov 15 '05 #1
5 1976
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

Nov 15 '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
Nov 15 '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
Nov 15 '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

Nov 15 '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

Nov 15 '05 #6

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

Similar topics

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...
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...
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?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.