views:

258

answers:

6

If I call a function like myObj.setType("fluid"); many times in a program, how many copies of the literal "fluid" are saved in memory? Can the compiler recognize that this literal is already defined and just reference it again?

+14  A: 

This has nothing to do with C++(the language). Instead, it is an "optimization" that a compiler can do. So, the answer yes and no, depending on the compiler/platform you are using.

@David This is from the latest draft of the language:

§ 2.14.6 (page 28)

Whether all string literals are distinct (that is, are stored in non overlapping objects) is implementation defined. The effect of attempting to modify a string literal is undefined.

The emphasis is mine.

In other words, string literals in C++ are immutable because modifying a string literal is undefined behavior. So, the compiler is free, to eliminate redundant copies.

BTW, I am talking about C++ only ;)

AraK
That's not entirely true. If he were using Ruby, say, the answer would definitely be no, because Ruby strings are always mutable.
David Seiler
David, how does it affect his answer regarding c++?
Michael Krelin - hacker
That's not what AraK said. He said it had nothing to do with C++, but with the compiler/environment. A different language will have different rules.
Michael Kohne
No one cares about the behavior of a Ruby implementation when asking about C/C++ code.
Chris Lutz
+4  A: 

Yes it can, but there's no guarantee that it will. Define a constant if you want to be sure.

David Seiler
+1 I would have added that "defining a constant" meant `const char cst[]="foo";` . The word "define" may evoke the wrong idea.
Pascal Cuoq
+6  A: 

Yes, it can. Of course, it depends on the compiler. For VC++, it's even configurable:

http://msdn.microsoft.com/en-us/library/s0s0asdt%28VS.80%29.aspx

Stu
A: 

I believe that in C/C++ there is no specified handling for that case, but in most cases would use multiple definitions of that string.

John Weldon
The standard does not require or preclude interned strings. Meyers talks about the trouble interned objects can cause in _Effective C++_ (it may actually be in _More Effective C++_).
KitsuneYMG
A: 

2.13.4/2: "whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation-defined".

This permits the optimisation you're asking about.

As an aside, there may be a slight ambiguity, at least locally within that section of the standard. The definition of string literal doesn't quite make clear to me whether the following code uses one string literal twice, or two string literals once each:

const char *a = "";
const char *b = "";

But the next paragraph says "In translation phase 6 adjacent narrow string literals are concatenated". Unless it means to say that something can be adjacent to itself, I think the intention is pretty clear that this code uses two string literals, which are concatenated in phase 6. So it's not one string literal twice:

const char *c = "a" "a";

Still, if you did read that "a" and "a" are the same string literal, then the standard requires the optimisation you're talking about. But I don't think they are the same literal, I think they're different literals that happen to consist of the same characters. This is perhaps made clear elsewhere in the standard, for instance in the general information on grammar and parsing.

Whether it's made clear or not, many compiler-writers have interpreted the standard the way I think it is, so I might as well be right ;-)

Steve Jessop
+1  A: 

This is a compiler implementation issue. Many compilers that I have used have an option to share or merge duplicate string literals. Allowing duplicate string literals speeds up the compilation process but produces larger executables.

Thomas Matthews