-1

I have stumbled on an interesting thing related to C-style strings.

Suppose I have this code:

    const char* str = "a";
    const char* str1 = "a";

    if (str == str1)
        cout <<"MAGIC HAPPENED!str="<<str<<", str1="<<str1 << endl;

If I understand correctly, if statement in this particular case should compare 2 pointers(since it is const char *, not the actual char of 1 byte), therefore the result should be false (since str and str1 are 2 different variables). Yet somehow I am getting result true - I see the output.

Any ideas why? :-)
NOTE: I am testing on Windows, with Qt's MINGW 11.2.0 compiler. And I am terribly sorry if this question was already answered.

  • 5
    _"...Whether string literals can overlap and whether successive evaluations of a string-literal yield the same object is unspecified. That means that identical string literals __may or may not__ compare equal when compared by pointer...."_ https://en.cppreference.com/w/cpp/language/string_literal – Richard Critten Aug 01 '23 at 12:50
  • Wording nitpick: it doesn't matter that `str` and `str1` are different variables, only whether their values are different. – HolyBlackCat Aug 01 '23 at 12:53
  • TL;DR: You're reasoning is correct. However, the standard doesn't guarantee that different literals will be unique, the idea being to allow compilers to optimize the storage of identical or overlapping literals. – Brian61354270 Aug 01 '23 at 12:53
  • Re: "not the actual `char` of 1 byte", note that it's actually "the actual `const char` array of length 2" – Brian61354270 Aug 01 '23 at 12:58
  • You could look at the numeric values of `str` and `str1` before writing for "magic". – i486 Aug 01 '23 at 12:59
  • The code is akin to `int a = 10; int b = 10; if (a == b) cout << "Magic happened!\n";`. The only unknown is whether the compiler "unifies" identical C-string literals or not. – Eljay Aug 01 '23 at 14:41

2 Answers2

2

In C-style strings, when you declare two string literals with the same content, the compiler may choose to optimize and point both variables to the same memory location. This is known as string pooling or string interning. Since both str and str1 have the same content ("a"), the compiler may decide to store both variables in the same memory location, effectively pointing to the same address. As a result, the if statement str == str1 will evaluate to true, and you'll see the output inside the if block.

const char* str = "a";
const char* str1 = "a";

However, keep in mind that string pooling is an optimization made by the compiler and might not always happen. For different compilers or compiler settings, you might get different results, and you should not rely on this behavior for comparing string literals. Instead, use strcmp or similar string comparison functions to compare the contents of C-style strings.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
2

It is a feature of your toolchain rather than of the C programming language. Most compilers will remove duplicate string literals and generate a single common reference.

See for example:

It is an optimisation that may not always be applied, so you cannot assume that your example will always behave in that way for all compilers or all build option combinations.

Clifford
  • 88,407
  • 13
  • 85
  • 165