实在是搞不明白为什么会出现 segmentation fault

2015 年 1 月 18 日
 yhf

为的是实现一个 LRU Cache. 用 hashmap 和双向链表实现,有两个接口:

T get(K key);
void put(K key, T data);

可是运行出现 segmentation fault,不知道是什么原因...

https://gist.github.com/yhfyhf/c11544185c1172650614

2278 次点击
所在节点    C
14 条回复
diablocy
2015 年 1 月 18 日
无条件执行 count++
bombless
2015 年 1 月 18 日
void put(...) {
if (node) {
...
}
else {
...
node->key = key;
...

看到没有,node的值是零,你非要给它赋值
diablocy
2015 年 1 月 18 日
恩,ls说的也是问题, 没有new node
msg7086
2015 年 1 月 18 日
我做leetcode的LRU Cache是用的unordered_map+list来做的。
一般没必要的话最好用STL而不是自己造轮子。
vietor
2015 年 1 月 18 日
临界区?
clowwindy
2015 年 1 月 18 日
noli
2015 年 1 月 18 日
gdb lldb 什么的……不是跟踪一下就有了吗?
yhf
2015 年 1 月 18 日
@bombless 非常感谢!
yhf
2015 年 1 月 18 日
@clowwindy 非常感谢!
yhf
2015 年 1 月 18 日
@clowwindy 最后这行 (Node<int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > *) $0 = 0x0000000000000000 是什么意思?
msg7086
2015 年 1 月 18 日
@yhf Node<int, string>*指针指向了null。
yuankui
2015 年 1 月 19 日
看成了 [实在是搞不明白为什么会出现 segmentfault.com] 。。
羞愧。
chenwl
2015 年 1 月 19 日
@yuankui 我也差点看错了~
acgeo
2015 年 1 月 19 日
多半数组越界

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.xtra.eu.org/t/163236

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX