删除堆内存的分配会导致崩溃
Assignment deleting heap memory causes crash
我正在写一个字符串类,在做赋值操作符重载时,我正在观察我们删除以前分配的内存的部分崩溃。我试着通过代码进行追踪,但没能弄清楚。任何指针都有帮助
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
)。
相关文章:
- c++11:在VS2015中分配全局STL "map<string, string>" initializer_list崩溃
- 分配内存并在回调时调用C++的 Rust 函数崩溃
- STD :: FSTREAM在使用预先分配的内存时在Main之后崩溃
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 在崩溃时释放分配的指针的正确方法
- PCL发布构建崩溃而无法分配
- 当我释放由CFFI生成的DLL分配的char*时,为什么我的应用程序会崩溃
- 为什么在复制构造函数中分配联合成员会崩溃
- 在构造函数[C ]中的FP分配上崩溃
- 矢量未正确擦除内容(复制分配运算符的量运行直到崩溃 [BEX])
- 程序在解除分配字符数组时崩溃
- 删除在不同函数中动态分配的对象时崩溃
- 如果在进程崩溃后在进程中分配内存,会发生什么情况
- CComPtrBase::~解除分配智能指针时,CComPtr Base崩溃
- 删除矢量元素指向的已分配内存会导致程序崩溃
- 矩阵分配导致崩溃
- 释放动态分配的内存时程序崩溃
- Boost.SSpirit.Qi在将规则分配给包括自身在内的序列时崩溃
- QString分配崩溃
- 程序崩溃(动态内存分配)