Can you depend on compilers that if you use the empty string literal "" all over the place that it only allocates space once for that string?
Can you depend on compilers that if you use the empty string literal "" all over the place that it only allocates space once for that string?
I don't think so, no.
Once... Or never... Or any time... It's upto compiler.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I would imagine that it would normally be once or never in most cases.
VC++ 2005 has an option for "String Pooling", which sounds like it does what you're asking, but I think it's disabled by default.
The short answer is "Probably".
The compiler can "merge" (or "fold") multiple constant strings into a single string - but usually only within that particular source file. Sometimes the linker is able to take it the full way to the whole executable, but not always.
I wouldn't worry too much about empty strings - each instance only takes up a byte. Long strings (a common case is format strings for printf - obviously not in a C++ program, but I'm sure people duplicate strings all over the program in "cout" or similar) that may be repeated many times.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
>> VC++ 2005 has an option for "String Pooling", which sounds like it does what you're asking, but I think it's disabled by default.
That's a little odd. The compiler should already be allowed to do that for string literals, so I expected that option to be for non string literals. But the example they have uses string literals.
I think the string pooling is slightly different, the example they give is:
With this option t and s would point to the same memory.Code:char *s = "This is a character buffer"; char *t = "This is a character buffer";
This is slightly different from string literals not assigned to pointers, e.g. as arguments to functions in various ways, where the address of the string isn't assigned to any variable.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I guess, but it shouldn't be. The standard already deprecates the assignment above to non-const char *, and if you modify the string pointed to by s or t that is undefined behavior, so there's no reason not to maintain only one copy of that string. If the example used this:then I might understand the point.Code:char s[] = "This is a character buffer"; char t[] = "This is a character buffer";
The reason string pooling is not enabled by default is backward compatibility with programs foolish enough to modify string literals. If the compiler merged strings, the modifications would show up in all strings.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I think that would make more sense if it was const char [].