Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strings in static memory instances count

As I know compiletime C-like strings are kept in static memory as only one instance. For instance I got both true on gcc 4.6 running example below. But I wonder is it always true and can be portable. Behavior on both C and C++ is interesting.

#include <iostream>

bool amIportable(const char* value) {
  const char* slocal = "Hello";
  return (slocal==value);
}

int main() {
  const char* s = "Hello";
  std::cout << std::boolalpha 
            << amIportable(s) << '\n'
            << amIportable("Hello") << '\n';
}
like image 1000
triclosan Avatar asked Jul 28 '13 14:07

triclosan


People also ask

How does the strings are stored in the memory?

Strings are stored on the heap area in a separate memory location known as String Constant pool. String constant pool: It is a separate block of memory where all the String variables are held. String str1 = "Hello"; directly, then JVM creates a String object with the given value in a String constant pool.

Are string literals static?

String literals have static storage duration, and thus exist in memory for the life of the program.

Where are strings stored in C?

When strings are declared as character arrays, they are stored like other types of arrays in C. For example, if str[] is an auto variable then the string is stored in stack segment, if it's a global or static variable then stored in data segment, etc.

Are string literals stored in stack or heap C?

String literals are not stored in the heap or the stack, they are stored directly in your program's binary. Literally embedded in the binary, and the reference is a reference to the location in the binary.


2 Answers

No, this is not always true, nor is it portable.

Merging identical string literals is an optimization that is performed by the compiler and the linker working together. Recent versions of both GCC and Microsoft's compiler both support it, but only when certain optimization switches are set.

And it's not just an "on" or "off" feature. Different compilers and different optimization settings will also affect how aggressively this is performed. For example, sometimes string literals are pooled only within the scope of an individual function, other times it happens at the level of the translation unit, and still other times the linker might get involved to do it across multiple translation units.

This is allowed because the C and C++ standards leave this behavior as implementation-dependent.

like image 53
Cody Gray Avatar answered Oct 02 '22 14:10

Cody Gray


No, it's implementation dependent for both C and C++.

C11 §6.4.5/7 String literals

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

C++11 §2.14.5/12 String literals

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

like image 45
Yu Hao Avatar answered Oct 02 '22 13:10

Yu Hao