日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构开发(7):典型问题分析(Bugfix)

發布時間:2023/12/13 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构开发(7):典型问题分析(Bugfix) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

0.目錄

1.創建異常對象時的空指針問題

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;} }

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);}}

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

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

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,也需要迭代升級,解決問題。

轉載于:https://www.cnblogs.com/PyLearn/p/10123468.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的数据结构开发(7):典型问题分析(Bugfix)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。