本文共 4066 字,大约阅读时间需要 13 分钟。
#include#include "Exception.h"using namespace std;using namespace DTLib;int main() { try { NullPointerException npe; cout << "throw" << endl; throw npe; } catch(const exception& e) { cout << "catch" << endl; } return 0;}
问题分析:
strdup函数默认假设输入的字符串不为空。如果传递空指针(0),会导致堆内存泄漏。Exception.h中,直接赋值m_message = message;可能导致外部字符串的生命周期控制不当。strdup复制字符串到堆空间,并检查是否为空。解决方案:
Exception.h中的赋值逻辑为:m_message = (message ? strdup(message) : NULL);
#include#include "LinkList.h"using namespace std;class Test : public Object { int m_id;public: Test(int id = 0) { m_id = id; } ~Test() { if(m_id == 1) { throw m_id; } }};int main() { LinkList list; Test t0(0), t1(1), t2(2); try { list.insert(t0); list.insert(t1); list.insert(t2); list.remove(1); } catch(int e) { cout << e << endl; cout << list.length() << endl; } return 0;}
问题分析:
remove()函数未处理异常,可能导致链表长度错误。解决方案:
remove()函数是否处理异常:bool remove(int i) { bool ret = (0 <= i && i <= m_length); if(ret) { Node* current = position(i); Node* toDel = current->next; if(m_current == toDel) { m_current = toDel->next; } current->next = toDel->next; m_length--; destroy(toDel); } return ret;}clear()函数中修改为循环删除所有节点。#include#include "LinkList.h"using namespace std;int main() { LinkList list; for(int i = 0; i < 5; i++) { list.insert(i); } for(list.move(0); !list.end(); list.next()) { if(list.current() == 3) { list.remove(list.current()); cout << list.current() << endl; } } for(list.move(0); !list.end(); list.next()) { cout << list.current() << endl; } return 0;}
问题分析:
m_current未指向下一个节点,导致随机值问题。解决方案:
remove()函数:bool remove(int i) { bool ret = (0 <= i && i <= m_length); if(ret) { Node* current = position(i); Node* toDel = current->next; if(m_current == toDel) { m_current = toDel->next; } current->next = toDel->next; m_length--; destroy(toDel); } return ret;}m_current指向下一个节点。#include#include "StaticLinkList.h"using namespace std;int main() { StaticLinkList list; for(int i = 0; i < 5; i++) { list.insert(i); } list.remove(3); for(int i = 0; i < list.length(); i++) { cout << list.get(i) << endl; } return 0;}
问题分析:
destroy()函数的逻辑存在问题,可能导致内存未正确归还。解决方案:
destroy()函数:void destroy(node* pn) { SNode* pst = dynamic_cast (pn); for(int i = 0; i < n; i++) { if(pst == (space + i)) { m_used[i] = 0; pst->~SNode(); break; } }} #include#include "LinkList.h"#include "StaticLinkList.h"using namespace std;int main() { StaticLinkList list; for(int i = 0; i < 5; i++) { list.insert(i); } for(int i = 0; i < 5; i++) { cout << list.get(i) << endl; } return 0;}
问题分析:
解决方案:
~StaticLinkList() { this->clear();}clear()函数调用子类的destroy(),避免多态问题。#include#include "DynamicArray.h"using namespace std;int main() { DynamicArray > d; d.resize(3); for(int i = 0; i < d.length(); i++) { d[i].resize(i + 1); } for(int i = 0; i < d.length(); i++) { for(int j = 0; j < d[i].length(); j++) { d[i][j] = i * j; } } for(int i = 0; i < d.length(); i++) { for(int j = 0; j < d[i].length(); j++) { cout << d[i][j] << " "; } cout << endl; } return 0;}
问题分析:
DynamicArray支持嵌套数组。解决方案:
DynamicArray构造嵌套数组,实现二维功能。以上问题通过深入分析和优化,确保了代码的健壮性和安全性。
转载地址:http://wcux.baihongyu.com/