После вызова new std::bad_alloc c++

MyString& MyString::insert(size_t index, const char* string, size_t count)
{
    if (count == 0)
        return *this;

    assert(string != nullptr);
    assert(index <= this->size());
    assert(count <= strlen(string));

    size_t length = this->size();
    size_t new_length = length + count;

    this->length_ = new_length;

    if (new_length >= this->capacity())
    {
        char* str = new char[new_length + 1]();
        assert(str != nullptr);

        memcpy(str + index, string, count);
        memcpy(str, this->str_, index);
        memcpy(str + index + count, this->str_ + index, length - index);

        if (this->str_ != nullptr)
            delete[] this->str_;
        this->str_ = str;

        this->capacity_ = new_length + 1;
    }
    else
    {
        memcpy(this->str_ + index + count, this->str_ + index, length - index + 1);
        memcpy(this->str_ + index, string, count);
    }

    return *this;
}

Выдает bad::bad_alloc после вызова new Компилирую на Unix, аргументы поступают правильные, память под объект на стеке


Ответы (0 шт):