yon*_*oni 4 c++ unordered-map segmentation-fault
编辑:解决了,我知道如何,但我不明白为什么.
我改变了variables声明
tr1::unordered_map<int,T> variables;
Run Code Online (Sandbox Code Playgroud)
至
unordered_map<int,T> variables;
Run Code Online (Sandbox Code Playgroud)
它工作正常.
如果你知道为什么请在答案中写下来.
我有一个非常大的程序,所以我不知道我应该带哪个代码.
有一个抽象类,它继承了派生类.摘要有unordered_map<int,int>(模板)作为私有成员和公共方法insert(int,int).
派生类使用基类insert方法将元素插入unordered_map<int,int>容器,
第一个int使用像计数器一样,从0开始.前十一个插入元素正常,但在第12个元素中我得到sigsegv,而struct equal_to在stl_function.h中错误(209).
在调试器中,我看到unordered_map的bucket_count等于11,也许它是某些东西的线索.
我的编译器是gcc 4.6.1.
也许你可以写一般可以导致sigsegv的内容unordered_map.insert?
谢谢,抱歉我的英语不好.
如果我知道哪个,我会带来具体的代码.
编辑:这是insert方法:
virtual void Insert(int arrayPlace, T value)
{
if (!isReadOnly)
{
if (IsValueDataValid(value))
{
variables[arrayPlace] = value;
}
else
{
throw 2;
}
}
else
{
throw 4;
}
};
Run Code Online (Sandbox Code Playgroud)
声明是:
tr1::unordered_map<int,T> variables;
Run Code Online (Sandbox Code Playgroud)
当arrayPlace== 11时,sigsegv会发生,并且无关紧要value.
这个问题的答案很简单:如果你正确使用代码,就不会创建分段错误std::unordered_map!所以问题就变成了:使用时典型的用户错误是什么std::unordered_map?副手我会立即想到三个问题:
T你得到的类型是否正确实现了复制构造.特别是,如果复制构造函数不在类中但是类具有赋值运算符或析构函数,则需要注意它.鉴于密钥是一个,int并且提供了哈希码和相等性,我将集中讨论第一个问题.也就是说,一旦我证明使用std::unordered_map确实是问题,我就会专注于此:分段违规也可能很容易因事先搞砸而导致.例如,某些东西可能会以错误的方式覆盖内存或删除内存等.像purify或valgrind这样的工具可以帮助找到这些问题.在任何情况下,您都希望将程序归结为最小的崩溃示例.通常我发现问题在这个过程中变得明显.