472,330 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 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 2047
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...
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...
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.