乙個list的 除錯 再思考 改進 1

2021-06-27 07:38:22 字數 1557 閱讀 1265

這段**目的:  構造有序鍊錶, 實現 merge 操作,將兩個有序表合併,並且去重.

例子:   

a:  1 2  2 3 

b:   1 3 5 8

合併得c

c: 1  2 3  5 8

下面**有一句錯了,導致段錯誤,

如何除錯呢/

#include #include class my_list

}; node* head;

node** find(int data); //get pointer to the 'next' member of node before data

node* pop();

public:

my_list();

void sorted_insert(int data);

my_list merge(my_list& b);

void show();

};my_list::node** my_list::find(int data)

return tmp;

}my_list::node* my_list::pop()

return tmp;

}my_list::my_list()

void my_list::sorted_insert(int data)

void my_list::show()

printf("null\n");

}my_list my_list::merge(my_list& b)

while(head!=null && b.head!=null)

else

if (tmp->data != last)

else

} while(head != null)

else

}while(b.head != null)

else

}return c;

}int main()

b.sorted_insert(30);

b.sorted_insert(1);

b.sorted_insert(8);

b.sorted_insert(3);

a.show();

b.show();

c = a.merge(b);

c.show();

a.show();

b.show();

for (int i=0; i<5; i++)

c = a.merge(b);

return 0;

}

使用gdb除錯core檔案

g++ -ggdb -o list list.cpp

ulimit -c unlimited

gdb --tui ./list core

bt可以看到時merge的時候, tmp 居然是null!

因為head都不是null,

所以可以判斷時 pop函式出問題了.

仔細觀察或者 監視pop的返回值,

可發現是 tmp重複定義了,因此 pop總是返回 null.

Thunk 技術的乙個改進

thunk 技術的乙個改進 摘要 介紹了 thunk 技術中如何避免直接寫機器碼。關鍵字 thunk 機器碼 this指標 thunk技術,一般認為是在程式中直接構造出可執行 的技術 在正常情況下,這是編譯器的任務 深度探索c 物件模型 中對這個詞的 有過考證 在中文版的162頁 說thunk是kn...

Thunk 技術的乙個改進

thunk 技術的乙個改進 摘要 介紹了 thunk 技術中如何避免直接寫機器碼。關鍵字 thunk 機器碼 this指標 thunk技術,一般認為是在程式中直接構造出可執行 的技術 在正常情況下,這是編譯器的任務 深度探索c 物件模型 中對這個詞的 有過考證 在中文版的162頁 說thunk是kn...

乙個晚上的思考

本來晚上來實驗室打算寫 沒想到因為瀏覽乙個帖子而花費了乙個晚上。源於csdn社群的一篇帖子 帖子標題比較惹眼。樓主是名武大的研究生,方向是gis。從發帖看來,的確對c c 有很深的了解,不過也有片面的一些地方,當然要比我強n倍。他已經簽下了份網易的offer,工作地點 廣州,試用期5k。我一直在想自...