473,569 Members | 2,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2142
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.co m
"Lecture Snoddddgrass" <ma***@hamburg. fry> wrote in message
news:eB******** ******@TK2MSFTN GP12.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.co m>
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.megan ewsservers.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******** ******@TK2MSFTN GP12.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
6574
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
5
1992
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 these two code snippets: Example A ======== const String hey="hey"; MessageBox.Show(hey); MessageBox.Show(hey); MessageBox.Show(hey);
7
14676
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 the const pointer (i.e. t)? I got a segmentation fault in the last for loop. Thanks in advance! #include <iostream> using namespace std;
16
10801
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
3515
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
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6283
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.