title: Why does my char* copier return different things?
body: <p>Writing a simple string copier and testing it in the main() fucntion. What's odd is that sometimes the program returns</p>
  
  <p>"HelloHello" </p>
  
  <p>like it should, but maybe every third time I run it, the program prints out:</p>
  
  <p>"Hello!Hello!▌▌▌▌▌▌▌▌▌▌▒"UòB╚"</p>
  
  <p>Why is the tail of garbage data only sometimes being added to the end of my second string?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int strlength(const char* c)
  {
      int size = 0;
      while (*c) {
          ++c;
          ++size;
      }
      return size;
  }
  
  char* mystrdup(const char* c)
  {
      int size = strlength(c);
      char* result = new char;
      copy(c, c + size, result);
      return result;      
  }
  
  void print_array(const char* c)
  {
      int size = strlength(c);
      while (*c) {
          cout &lt;&lt; *c;
          ++c;
      }
  }
  
  int main()
  {
      char test[] = "Hello!";
      char* res = mystrdup(test);
      print_array(test);
      print_array(res);
  }
  
  </code></pre>