title: Self-made ugly vector
body: <p>I've got some training example of selt-made vector, it's not template for simplicity:</p>
  
  <pre><code>class UglyStringsVector {
  public:
    UglyStringsVector() = default;
    explicit UglyStringsVector(size_t size);
    UglyStringsVector(const UglyStringsVector&amp;);
    ~UglyStringsVector();
  
    std::string &amp;operator[](size_t index);
  
    std::string *begin();
    std::string *end();
  
    const std::string *begin() const;
    const std::string *end() const;
  
    size_t Size() const;
    size_t Capacity() const;
  
    void PushBack(std::string value);
    void operator=(const UglyStringsVector &amp;other);
  
  private:
    std::string *data = nullptr;
    size_t size = 0;
    size_t capacity = 0;
  
    void ExpandIfNeeded();
  };
  </code></pre>
  
  <p>Assignment operator is not implemented correctly:</p>
  
  <pre><code>UglyStringsVector&amp; UglyStringsVector::operator=(const UglyStringsVector &amp;other) {
    delete[] data;
    data = new std::string[other.capacity];
    size = other.size;
    capacity = other.capacity;
    copy(other.begin(), other.end(), begin());
    return *this;
  }
  </code></pre>
  
  <p>As we see here, when <code>this == &amp;other</code> (<em>I don't check this condition on purpose of question</em>), it deletes its own memory (<em>of course this is wrong</em>), and then reallocates new strings on the same place (<code>data = new std::string[other.capacity];</code>), strings are not uninitialized, because default constructor was called during <code>operator new[]</code>, and then copy strings to themselves (<code>copy(other.begin(), other.end(), begin());</code>).</p>
  
  <p>Lets imagine than losing memory is not problem :-)
  Something whispers to me that copying memory to itself is undefined behavior, but I'm not sure.
  The question: <strong><em>is there any undefined behavior?</em></strong></p>