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

2015-01-18 16:51:32 +08:00
 yhf

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

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

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

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

1588 次点击
所在节点    C
14 条回复
diablocy
2015-01-18 17:08:50 +08:00
无条件执行 count++
bombless
2015-01-18 17:16:22 +08:00
void put(...) {
if (node) {
...
}
else {
...
node->key = key;
...

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

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

https://www.v2ex.com/t/163236

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

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

© 2021 V2EX