title: What is the lifetime of the result of std::string::c_str()?
body: <p>In one of my programs, I have to interface with some legacy code that works with <code>const char*</code>.</p>
  
  <p>Lets say I have a structure which looks like:</p>
  
  <pre><code>struct Foo
  {
    const char* server;
    const char* name;
  };
  </code></pre>
  
  <p>My higher-level application only deals with <code>std::string</code>, so I thought of using <code>std::string::c_str()</code> to get back <code>const char*</code> pointers.</p>
  
  <p>But what is the lifetime of <code>c_str()</code> ?</p>
  
  <p>Can I do something like this without facing undefined behavior ?</p>
  
  <pre><code>{
    std::string server = "my_server";
    std::string name = "my_name";
  
    Foo foo;
    foo.server = server.c_str();
    foo.name = name.c_str();
  
    // We use foo
    use_foo(foo);
  
    // Foo is about to be destroyed, before name and server
  }
  </code></pre>
  
  <p>Or am I supposed to immediately copy the result of <code>c_str()</code> to another place ?</p>
  
  <p>Thank you.</p>