1.解引用已经delete过的对象
list& list::unique(void) {
// list l(*this);
// listPointer temp1 = l.head;
listPointer temp1 = head;
// int counter = 0;
while (temp1 != NULL) {
listPointer temp2 = head;
int count = 0;
while (temp2 != NULL) {
listPointer tem3=tem2->next;
if (temp1->data == temp2->data && temp1 != temp2) {
erase(count);
--count;
}
++count;
//temp2 = temp2->next;//此处的tem2可能已经被delete了,不能再解引用
tem2=tem3;
}
temp1 = temp1->next;
}
return *this;
}
list& list::remove_if(bool (*condition)(listPointer)) {
listPointer temp = head;
int count = 0;
// std::cout << (condition == NULL) << std::endl;
while (temp != NULL) {
listPointer p = temp->next;
if (condition(temp)) {
//std::cout << " count " << count << " data " << temp->data << std::endl;
erase(count);
--count;
}
temp = p;//此处不能直接解引用来取next
++count;
}
return *this;
}
在可能delete内存时,不能再解引用
每次delete后都要设为nullptr
2.数组越界
void String::append(const char &other) {
if (other == '\0') {
return;
}
if (_size <= 1+_length) {//_size<=_length
_size = 2*_size;
String ss(_buff);
delete[] _buff;
_buff = new char[_size];
for (int i = 0; i < ss._length; i++) {
_buff[i] = ss._buff[i];
}
}
_buff[_length] = other;
++_length;
_buff[_length] = '\0';
}
考虑特殊情况,大小相等,等于0…
数组位置一定要画图分析
不想思考的话,开到足够大
3.递归深度太大,系统栈满
使用迭代或有序搜索减少递归深度
4.栈空间开辟过大数组
声明为静态数组