删除堆内存的分配会导致崩溃

Assignment deleting heap memory causes crash

本文关键字:崩溃 分配 内存 删除      更新时间:2023-10-16

我正在写一个字符串类,在做赋值操作符重载时,我正在观察我们删除以前分配的内存的部分崩溃。我试着通过代码进行追踪,但没能弄清楚。任何指针都有帮助

str& str::operator=(const str &Rhs)
{
    if (this != &Rhs)
    {
        cout << " attempt of self allocation - " << endl;
        **delete[] this->_element;**   // crashes here
        this->_capacity = Rhs._capacity;
        this->_display = Rhs._display;
        this->_size = Rhs._size;

        if (Rhs._size > 0)
        {
            this->_element = new char(this->_size);
            if (this->_element == NULL)
            {
                cout << " mem allocation failed " << endl;
            }
        }
        for (int counter = 0; counter <= this->_size; counter++)
        {
            this->_element[counter] = Rhs._element[counter];
        }
    }
    return *this;
}
/*copy constructor */
str::str(const str& Rhs)
{
    // copy constructor called 
    this->_capacity = Rhs._capacity;
    this->_display = Rhs._display;
    this->_size = Rhs._size;
    if (Rhs._size > 0)
    {
        this->_element = new char(_size);
        if (this->_element == NULL)
        {
            cout << " mem allocation failed " << endl;
        }
        for (int counter = 0; counter <= this->_size; counter++)
        {
            this->_element[counter] = Rhs._element[counter];
        }
    }
}

/* constructor */

str::str(const char *Y)
{
    cout << "constructor called !!! -- " <<  endl;
    size_t len = this->stringlen(Y);
    this->_element = new char(len + 1);
    for (int counter = 0; counter < len; counter++)
    {
        this->_element[counter] = Y[counter];
    }
    this->_element[len] = '';
    this->_size = len + 1;
    cout << "string in constructor is -- " << this->_element << endl;
}

From .h file

class str 
{
public:
    /*Default Constructor*/
    explicit str();
    /*Constructor with single char Argument*/
    explicit str(char x);
    /*Constructor with char array Argument*/
    explicit str(const char* Y);
    /* Creating new element with copy constructor */
    str(const str& Rhs);
    /* Overloading of Assignment operator */ 
    str& operator=(const str& Rhs);
    friend int string_compare(const str& Lhs, const str& Rhs);
    int reverse();
    size_t stringlen(const char* Y);
    str& operator+(str& Rhs);
    bool operator==(const str& Rhs);
    bool operator!=(const str& Rhs);
    friend ostream& operator<<(ostream &out, str& Lhs);

private:
char*  _element;
int _capacity;
bool _display;
int _size; //largest accessed + 1
};

主程序-

void test1() {
  const char *j = "abc";
  cout << "length of j = " << strlen(j) << endl;
  str s1('U');
  str s2("hello");
  cout << s2 << endl;
  s2.reverse();
  cout << s2 << endl;
  str s3(s2);
  cout << s1 << endl;
  cout << s2 << endl;
  cout << s3 << endl;
  **s2 = s1;**  // crashes
  cout << s2 << endl;
  cout << s1 << endl;

}

你的代码有几个问题。

  • 最大的一个是:如果你想分配一个数组,你需要使用new char[_size]new char(_size)分配一个字符,其值设置为_size

  • 第二,一旦你解决了这个问题,你写过去你分配的数组的结束-判断在你的头注释你需要分配char[_size + 1]

  • 第三,在你的复制构造函数中,你永远不会初始化_element数组,在你的赋值操作符中,你永远不会清除_element值。这将最终导致崩溃,当你复制或分配一个空的str,然后试图分配给它(或在销毁时,我假设析构函数也调用delete)。