title: Implement a class want to simulate std::string but get stuck when push_back it into a vector
body: <p>I have implemented a class String. When I push_back a String object to a Vector, the program gets stuck. Please help me review it if you're interested.</p>
  
  <p>String.h</p>
  
  <pre><code>#pragma once
  #include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include &lt;string&gt;
  
  class String {
  public:
     String();
     String(const char *);
     String(const String &amp;);
     String(const std::string &amp;);
     String(String &amp;&amp;) noexcept;
     String &amp;operator=(String &amp;&amp;) noexcept;
     ~String();
     String &amp;operator=(const String &amp;);
     std::string to_string() const;
  
     friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, String s);
  
  private:
     std::pair&lt;char *, char *&gt;
        alloc_n_copy(char *, char *);
     void free();
     static std::allocator&lt;char&gt; alloc;
     char *beg;
     char *end;
     size_t length;
  };
  </code></pre>
  
  <p>String.cpp</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;cstring&gt;
  #include "String.h"
  
  std::allocator&lt;char&gt; String::alloc = std::allocator&lt;char&gt;();
  
  String::String()
     : beg(nullptr), end(nullptr), length(0) {}
  
  String::String(const char *ptr) {
     std::cout &lt;&lt; "Const char constructor execute" &lt;&lt; std::endl;
     const char *tmp = ptr;
     while (*tmp++ != '\0') ++length;
     beg = alloc.allocate(length);
     end = std::uninitialized_copy(ptr, tmp, beg);
  }
  
  String::String(const std::string &amp;s) {
     std::cout &lt;&lt; "Const string constructor execute" &lt;&lt; std::endl;
     strcpy_s(beg, s.size(), s.c_str());
     length = s.size();
     char *tmp = beg;
     end = tmp + length;
  }
  
  String::String(const String &amp;s) {
     std::cout &lt;&lt; "Copy constructor execute" &lt;&lt; std::endl;
     beg = alloc.allocate(s.length);
     end = std::uninitialized_copy(s.beg, s.end, beg);
  }
  
  std::pair&lt;char *, char *&gt;
  String::alloc_n_copy(char *beg, char *end) {
     auto newBeg = alloc.allocate(end - beg);
     return{ newBeg, std::uninitialized_copy(beg, end, newBeg) };
  }
  
  String &amp;String::operator=(const String &amp;s) {
     length = s.length;
     auto newStr = alloc_n_copy(s.beg, s.end);
     free();
     beg = newStr.first;
     end = newStr.second;
     return *this;
  }
  
  String::String(String &amp;&amp;s) noexcept : beg(s.beg), end(s.end), length(s.length) {
     std::cout &lt;&lt; "Move constructor execute" &lt;&lt; std::endl;
     s.beg = s.end = nullptr;
     s.length = 0;
  }
  
  String &amp;String::operator=(String &amp;&amp;s) noexcept {
     if (this != &amp;s) {
        beg = s.beg;
        end = s.end;
        length = s.length;
        s.beg = s.end = nullptr;
        s.length = 0;
     }
     return *this;
  }
  
  void String::free() {
     while (length-- &gt;= 0) {
        alloc.destroy(end--);
     }
     alloc.deallocate(beg, length);
  }
  
  String::~String() {
     free();
  }
  
  std::string String::to_string() const {
     std::string s(beg);
     return s;
  }
  
  std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, String s) {
     std::string str(s.beg);
     os &lt;&lt; str;
     return os;
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>int main()
  {
     vector&lt;String&gt; v;
     String s1("abc");
     String s2("def");
  
     v.push_back(s1);
     v.push_back(s2);
  
     return 0;
  }
  </code></pre>
  
  <p>result:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Const char constructor execute
  Const char constructor execute
  Copy constructor execute
  Move constructor execute</code></pre>
  </div>
  </div>
  </p>
  
  <p>I don't know why the second push_back is a move construction.</p>
  
  <p>And when the push_back finished, the program can't exit. Is there any resource failed to release?</p>
  
  <p>Thanks </p>