title: C++ Length of char* does not make sense?
body: <p>I encounter this problem twice today, and I don't know where is the bug nor how to fix it.</p>
  
  <pre><code>#include &lt;string.h&gt;
  
  char *str1 = "ab";
  int length = strlen(str1);
  char *str2 = new char[length + 1];
  </code></pre>
  
  <p>So when I test it with a few more lines:</p>
  
  <pre><code>cout &lt;&lt; "Length of str1 = " &lt;&lt; strlen(str1) &lt;&lt; "\n";
  cout &lt;&lt; "Value of int length = " &lt;&lt; length &lt;&lt; "\n";
  cout &lt;&lt; "Length of str2 = " &lt;&lt; strlen(str2) &lt;&lt; "\n";
  </code></pre>
  
  <p>It gives me:</p>
  
  <p>Length of str1 = 2</p>
  
  <p>Value of int length = 2</p>
  
  <p>Length of str2 = 48</p>
  
  <p>I don't understand, 2 + 1 should be 3. The Length of str2 does not match what I initialize it.
  How can I fix this, please help, thanks.</p>