【发布时间】:2012-01-07 22:07:11
【问题描述】:
编辑:解决了,我知道怎么做,但我不明白为什么。
我将variables声明从
tr1::unordered_map<int,T> variables;
到
unordered_map<int,T> variables;
它工作正常。
如果你知道原因,请将其写在答案中。
我有一个非常大的程序,所以我不知道我应该把哪个代码带到这里。
有抽象类,继承派生类。
摘要有unordered_map<int,int>(模板)作为私有成员,公共方法insert(int,int)。
派生类使用基类insert方法向unordered_map<int,int>容器中插入元素,
第一个 int 使用 like 计数器并从 0 开始。前 11 个插入元素正常。但在第 12 个元素中,我得到了 sigsegv,并且在 stl_function.h(209) 处的 struct equal_to 出现故障。
在调试器中我看到 unordered_map 的 bucket_count 等于 11,这可能是一些线索。
我的编译器是 gcc 4.6.1。
也许您可以概括地写出什么可以在unordered_map.insert 中导致 sigsegv?
谢谢你,对不起我的英语不好。
我会带上具体的代码,如果我知道的话。
编辑:
这是insert 方法:
virtual void Insert(int arrayPlace, T value)
{
if (!isReadOnly)
{
if (IsValueDataValid(value))
{
variables[arrayPlace] = value;
}
else
{
throw 2;
}
}
else
{
throw 4;
}
};
声明是:
tr1::unordered_map<int,T> variables;
sigsegv 发生在 arrayPlace==11 时,而 value 是否相等并不重要。
【问题讨论】:
-
您错误地使用了容器,但没有导致问题的代码,没有人可以回答这个问题。如果有很多代码,请开始将问题减少到重现问题所需的最低限度。
-
假设it's always your fault 除非你能证明不是这样。
-
错误出现在您的代码中的其他地方。这样的无序地图没有任何问题。
标签: c++ segmentation-fault unordered-map