0.目錄
1.創建異常對象時的空指針問題
2.LinkList 中的數據元素刪除
3.LinkList 中遍歷操作與刪除操作的混合使用
4.StaticLinkList 中數據元素刪除時的效率問題
5.StaticLinkList 是否需要提供析構函數?
6.StLib 是否有必要增加多維數組類?
1.創建異常對象時的空指針問題
ISSUE 1——創建異常對象時的空指針問題:
改進Exception.cpp:
在init函數中改進strdup的賦值方法
void Exception::init(const char* message, const char* file, int line)
{/* message指向的字符串有可能在棧上,有可能在堆空間,還有可能在全局數據區* strdup()將字符串復制一份到堆空間中* file:發生異常的文件名* line:發生異常的行號* m_location的長度加2,一個給":",一個給"\0"*/m_message = (message ? strdup(message) : NULL);if( file != NULL ){char sl[16] = {0};itoa(line, sl, 10);m_location = static_cast<char*>(malloc(strlen(file) + strlen(sl) + 2));if( m_location != NULL ){m_location = strcpy(m_location, file);m_location = strcat(m_location, ":");m_location = strcat(m_location, sl);}}else{m_location = NULL;}
}
2.LinkList 中的數據元素刪除
ISSUE 2——LinkList 中的數據元素刪除:
單鏈表的實現沒有考慮異常安全性!
改進LinkList.h中的remove函數和clear函數:
bool remove(int i){bool ret = ((0 <= i) && (i < m_length));if( ret ){Node* current = position(i);Node* toDel = current->next;current->next = toDel->next;m_length--;destroy(toDel);}return ret;}void clear(){while ( m_header.next ){Node* toDel = m_header.next;m_header.next = toDel->next;m_length--;destroy(toDel);}}
3.LinkList 中遍歷操作與刪除操作的混合使用
ISSUE 3——LinkList 中遍歷操作與刪除操作的混合使用:
混合使用導致了m_current指向了已經被刪除的結點,于是程序出錯。
改進LinkList.h中的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;}
main.cpp測試
#include <iostream>
#include "LinkList.h"using namespace std;
using namespace StLib;int main()
{LinkList<int> 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.find(list.current()));cout << list.current() << endl;}}for(int i=0; i<list.length(); i++){cout << list.get(i) << endl;}return 0;
}
運行結果為:
4
0
1
2
4
4.StaticLinkList 中數據元素刪除時的效率問題
ISSUE 4——StaticLinkList 中數據元素刪除時的效率問題:
改進LinkList.h中的destroy函數:
void destroy(Node* pn){SNode* space = reinterpret_cast<SNode*>(m_space);SNode* psn = dynamic_cast<SNode*>(pn);for(int i=0; i<N; i++){if( psn == (space + i) ){m_used[i] = 0;psn->~SNode();break;}}}
main.cpp測試
#include <iostream>
#include "StaticLinkList.h"using namespace std;
using namespace StLib;int main()
{StaticLinkList<int, 10> 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;
}
運行結果為:
0
1
2
4
5.StaticLinkList 是否需要提供析構函數?
ISSUE 5——StaticLinkList 是否需要提供析構函數?:
構造函數與析構函數不會發生多態,于是調用的是父類的析構函數!
改進StaticLinkList.h:
在子類StaticLinkList中實現析構函數:
~StaticLinkList(){this->clear();}
6.StLib 是否有必要增加多維數組類?
ISSUE 6——StLib 是否有必要增加多維數組類?:
多維數組的本質:數組的數組!
不需要定義多維數組!
使用DynamicArray創建多維數組:
#include <iostream>
#include "DynamicArray.h"using namespace std;
using namespace StLib;int main()
{DynamicArray< DynamicArray<int> > 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;
}
運行結果為:
0
0 1
0 2 4
實踐經驗:
- 是軟件就有bug,因此需要不停的迭代升級,解決問題。庫是一種特殊的軟件產品,也會存在各種bug,也需要迭代升級,解決問題。