473,402 Members | 2,061 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,402 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 2123
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";
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.