2

I have read following in The C++ Programming Language special 3rd edition that:

Whether two identical character literals are allocated as one is implementation defined (§C.1).

const char* p="Heraclitus";
const char* q="Heraclitus";

void g ()
{
       if (p == q ) cout << "one!\n"; // result is implementation defined
       // ...
}

Note that == compares addresses (pointer values) when applied to pointers, and not the values pointed to.

I have tried following program on gcc 4.8.1 & MSVS 2010

#include <iostream>
int main()
{
    const char* p="Heraclitus";
    const char* q="Heraclitus";
    if(p==q)
        std::cout<<"fine!!!";
    else
        std::cout<<"!fine";
}

Output:

fine!!! (on gcc 4.8.1)

!fine (on MSVS 2010)

Why this is left as implementation defined behavior? What is the reason?

Destructor
  • 14,123
  • 11
  • 61
  • 126
  • [String Literal address across translation units](http://stackoverflow.com/q/26279628/1708801) has useful background information – Shafik Yaghmour Jun 15 '15 at 17:36
  • @Closers: Who voted to close? What's wrong in question? – Destructor Jun 15 '15 at 17:38
  • Aside: Enable [string pooling `/GF`](https://msdn.microsoft.com/en-us/library/s0s0asdt.aspx) in VC++ to match the behaviour of g++. It's off by default. – legends2k Jun 15 '15 at 17:42
  • @meet, nothing wrong with the question. It was closed because it was considered a duplicate of a previously asked and answered question. Whether the compiler chooses to use the same memory or not for a string constant is implementation dependent and not driven by a semantic requirement of the language. So the compiler may choose to use the same memory for reasons of memory use efficiency. But the programmer shouldn't depend upon that being the case. – lurker Jun 15 '15 at 17:48
  • @Barry those are not duplicates, I could remove the rationale from my answer and it would still answer that question and no longer be relevant to this one. They are asking two different question 1) can I rely on the behavior 2) why is the behavior that way. – Shafik Yaghmour Jun 15 '15 at 17:59
  • It is implementation defined because you are comparing *pointers* to memory; you are not comparing contents. The placement of the literals is up to the build process *and the operating system*. The compiler may or may not decide to place duplicate literals at the same memory location. The tool that loads the program into memory may consolidate duplicate identifiers. The behavior differs depending on the platform. Small embedded systems would like literals consolidated. Systems with lot's of memory, don't care. – Thomas Matthews Jun 15 '15 at 18:21
  • Try prefixing both variable definitions with `static`. The `static` keyword tells the compiler that there is only one instance, so it will have a better chance of consolidating the literals. – Thomas Matthews Jun 15 '15 at 18:24

1 Answers1

2

We can find a rationale in this comp.std.c thread: History question: String literals and it says:

GCC might have served as an example but not as motivation. Partly the desire to have string literals in ROMmable data was to support, er, ROMming. I vaguely recall having used a couple of C implementations (before the X3J11 decision was made) where string literals were either automatically pooled or stored in a constant data program section. Given the existing variety of practice and the availability of an easy work-around when the original UNIX properties were wanted, it seemed best to not try to guarantee uniqueness and writability of string literals.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740