title: string allocation in C++: why does this work?
body: <pre><code>void changeString(const char* &amp;s){
      std::string str(s);
      str.replace(0, 5, "Howdy");
      s = str.c_str();
  }
  
  int main() {
      const char *s = "Hello, world!";
      changeString(s);
      std::cout &lt;&lt; s &lt;&lt; "\n";
      return 0;
  }
  </code></pre>
  
  <p>When I run this code, it prints "Howdy, world!" I would think that <code>str</code> gets destroyed when <code>changeString</code> exits. Am I missing something with the way <code>std::string</code> gets allocated?</p>