delete(cur)
时间: 2024-05-30 16:07:31 浏览: 14
delete(cur)是C++中的动态内存释放方式,用于释放指针所指向的内存空间,其中cur是一个指针。使用new运算符创建的内存空间在使用完之后需要手动释放,否则会出现内存泄漏的问题。使用delete释放内存空间时,需要保证指针cur所指向的内存空间确实是使用new运算符分配得到的,否则会导致程序运行时错误。同时,在释放完内存空间后,建议将指针赋值为nullptr,以避免野指针的出现。
相关问题
#include <iostream> #include "listnode.h" using namespace std; /*struct listNode { listNode * prev, * next; int val; listNode(): val(0), prev(nullptr), next(nullptr){} listNode(int v, listNode *p, listNode *n): val(v), prev(p), next(n) { if (prev != nullptr) prev->next = this; if (next != nullptr) next->prev = this; } };*/ class OrderedList { protected: listNode * root; public: OrderedList(){root=new listNode();} ~OrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;} delete cur;} virtual void insert(int val)=0; void printList() const { listNode* cur=root; while(cur->prev!=nullptr)cur=cur->prev; while(cur!=nullptr){cout<<cur->val<<' ';cur=cur->next;} cout<<endl; } }; class AscendOrderedList:public OrderedList { public: AscendOrderedList(){root=new listNode();} ~AscendOrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;} delete cur;} void insert(int v) { if(root->next==nullptr&&root->prev==nullptr){root->val=v;return;} listNode* pre = root; while(pre->prev!=nullptr)pre=pre->prev; if(v<=pre->val){listNode*ins=new listNode();ins->val=v;ins->next=pre;return;} while (pre->next != nullptr && v > pre->val) { pre = pre->next; } listNode*a=pre->next; listNode*ins=new listNode(v, pre, pre->next); ins->prev=pre;ins->next=a; } }; class DescendOrderedList:public OrderedList { public: DescendOrderedList(){root=new listNode();} ~DescendOrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;} delete cur;} virtual void insert(int v){ if(root->next==nullptr&&root->prev==nullptr){root->val=v;return;} listNode*pre=root; while(pre->prev!=nullptr)pre=pre->prev; if(v>=pre->val){listNode*ins=new listNode();ins->val=v;ins->next=pre;return;} while(v<pre->val&&pre!=nullptr){pre=pre->next;} listNode*a=pre->next; listNode*ins=new listNode(v, pre, pre->next); ins->prev=pre;ins->next=a; } };输出错误
在代码中,构造函数和析构函数中的删除操作存在问题。在析构函数中,应该先删除链表中的节点,再删除root节点,否则会造成内存泄漏。同时,在构造函数中,应该将root节点的prev和next指针设为nullptr,否则会导致插入操作时出现问题。另外,在DescendOrderedList的insert函数中,应该先判断pre是否为nullptr,否则在pre为nullptr时会出现访问pre->val的错误。
#include <iostream> #include "listnode.h" using namespace std; struct listNode { listNode * prev, * next; int val; listNode(): val(0), prev(nullptr), next(nullptr){} listNode(int v, listNode *p, listNode *n): val(v), prev(p), next(n) { if (prev != nullptr) prev->next = this; if (next != nullptr) next->prev = this; } }; class OrderedList { protected: listNode * root; public: OrderedList(){root=new listNode();} ~OrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;}} virtual void insert(int val)=0; void printList() const { listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){cout<<cur->val<<' ';cur=cur->next;} } }; class AscendOrderedList:public OrderedList { public: AscendOrderedList(){root=new listNode();} ~AscendOrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;}} virtual void insert(int v){ listNode*pre=root; while(v>pre->val&&pre!=nullptr){pre=pre->next;} listNode*ins=new listNode(v, pre,pre->next); pre->next=ins; ins->next->prev=ins; } }; class DescendOrderedList:public OrderedList { public: DescendOrderedList(){root=new listNode();} ~DescendOrderedList(){ listNode* cur=root; if(cur->prev!=nullptr)cur=cur->prev; if(cur!=nullptr){listNode*tmp=cur;cur=cur->next;delete tmp;}} virtual void insert(int v){ listNode*pre=root; while(v<pre->val&&pre!=nullptr){pre=pre->next;} listNode*ins=new listNode(v, pre, pre->next); pre->next=ins; ins->next->prev=ins; } }为什么运行崩溃
这段代码中可能会有多个问题,具体原因需要根据您的输入、程序运行环境以及错误提示等来确定。以下是一些可能的问题和建议:
1. 在 `listNode` 的构造函数中,需要判断 `prev` 和 `next` 是否为空指针,才能为它们的 `prev` 和 `next` 成员变量赋值。修改代码如下:
```
listNode(int v, listNode* p, listNode* n) : val(v), prev(p), next(n) {
if (prev != nullptr) {
prev->next = this;
}
if (next != nullptr) {
next->prev = this;
}
}
```
2. 在 `OrderedList` 和其子类的析构函数中,需要循环删除链表中的所有节点。修改代码如下:
```
~OrderedList() {
listNode* cur = root;
while (cur->next != nullptr) {
cur = cur->next;
delete cur->prev;
}
delete cur;
}
~AscendOrderedList() {
listNode* cur = root;
while (cur->next != nullptr) {
cur = cur->next;
delete cur->prev;
}
delete cur;
}
~DescendOrderedList() {
listNode* cur = root;
while (cur->next != nullptr) {
cur = cur->next;
delete cur->prev;
}
delete cur;
}
```
3. 在 `AscendOrderedList` 和 `DescendOrderedList` 的 `insert()` 方法中,需要先判断当前节点是否为空指针,否则会出现访问空指针的错误。修改代码如下:
```
void insert(int v) {
listNode* pre = root;
while (pre->next != nullptr && v > pre->val) {
pre = pre->next;
}
listNode* ins = new listNode(v, pre, pre->next);
pre->next = ins;
if (ins->next != nullptr) {
ins->next->prev = ins;
}
}
```
```
void insert(int v) {
listNode* pre = root;
while (pre->next != nullptr && v < pre->val) {
pre = pre->next;
}
listNode* ins = new listNode(v, pre, pre->next);
pre->next = ins;
if (ins->next != nullptr) {
ins->next->prev = ins;
}
}
```
以上是可能的问题和建议,如果您还有其他问题或者需要更多帮助,请提供更多详细的信息和错误提示,以便我们更好地帮助您解决问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)