title: Difference between strlen(str.c_str()) and str.length() for std::string
body: <p>As an implicit understanding, I always thought that every implementation of <code>std::string</code> necessarily must satisfy <code>strlen(str.c_str()) == str.length()</code> for every string <code>str</code>.</p>
  
  <p>What does the C++ standard say about this? (Does it?)</p>
  
  <p><em>Background:</em> At least the implementations shipped with Visual C++ and gcc do not have this property.  Consider this example (see <a href="http://ideone.com/kWM3VV" rel="noreferrer">here for a live example</a>):</p>
  
  <pre><code>// Output:
  // string says its length is: 13
  // strlen says: 5
  #include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;
  
  int main() {
    std::string str = "Hello, world!";
    str[5] = 0;
    std::cout &lt;&lt; "string says its length is: " &lt;&lt; str.length() &lt;&lt; std::endl;
    std::cout &lt;&lt; "strlen says: " &lt;&lt; strlen(str.c_str()) &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>
  
  <p>Of course, the writing operation without <code>str</code> noticing is causing "the problem". But that's not my question. I want to know what the standard has to say about this behavior.</p>