问题描述
对于一个项目,我有一个开始表,我决定将其放入std::unordered_map
.不幸的是,我只能硬编码整个地图.所以,我决定将初始化拆分成多个文件.
For a project, I have an opening table that I decided to put in a std::unordered_map
. Unfortunately, I am restricted to hard-coding the entire map. So, I decided to split the initialization into multiple files.
class OpeningBook
{
public:
OpeningBook();
private:
std::unordered_map<std::string, int> opening_database_;
void init1();
void init2();
void init3();
void init4();
void init5();
};
并且构造函数只调用 init
函数:
and the constructor just calls the init
functions:
OpeningBook::OpeningBook()
{
init1();
init2();
init3();
init4();
init5();
}
所有这些看起来都像这样:
All of which just look like this:
void OpeningBook::init1()
{
opening_database_.insert(std::pair<std::string, int>("0001000000-10000001000000-1000001100000-1-1000",5000));
opening_database_.insert(std::pair<std::string, int>("0001000000-10000001000000-1000000100000-1-1100",5000));
opening_database_.insert(std::pair<std::string, int>("0001000000-10000001000000-1000000100001-1-1000",5000));
opening_database_.insert(std::pair<std::string, int>("0001000000-10000001000000-1000000100000-1-1010",5000));
opening_database_.insert(std::pair<std::string, int>("0001000000-10000001000000-1000000100010-1-1000",5000));
opening_database_.insert(std::pair<std::string, int>("0001000000-10000001000000-1000000100000-1-1001",5000));
opening_database_.insert(std::pair<std::string, int>("0001000000-10000001000000-100000010000-11-1000",0));
opening_database_.insert(std::pair<std::string, int>("0001000000-10000001000000-100000010000-10-1100",5000));
// continues
}
然而,只要我的代码遇到 init1()
中的左大括号,它就会抛出堆栈溢出异常.我认为不会发生堆栈溢出,因为 unordered_map
在堆上.这是怎么回事?我该怎么做才能解决这个问题?
However, as soon as my code hits the opening brace in init1()
, it throws a stack overflow exception. I thought that a stack overflow couldn't occur because an unordered_map
is on the heap. What's going on? and what can I do to fix this?
推荐答案
您在每个 initx()
方法中插入了多少项?如果有数千个,那么编译器生成的代码可能会使用堆栈上的大量临时变量,并且只是要求提供比可用空间更多的堆栈空间.
How many items are you inserting in each initx()
method? If it's many thousands, then it's possible that the compiler is generating code that uses a large number of temporaries on the stack, and simply asks for more stack space than there is available.
尝试进一步拆分您的初始化方法,看看是否能解决问题.
Try splitting your initialisation methods up further and see if that solves the problem.
更好的方法可能是拥有一个包含初始化数据的表:
A better approach might be to have a table which contains the initialisation data:
static const struct {
const char *str;
int n;
} DatabaseInitData[] = {
{"0001000000-10000001000000-1000001100000-1-1000",5000},
{"0001000000-10000001000000-1000000100000-1-1100",5000},
{"0001000000-10000001000000-1000000100001-1-1000",5000},
// etc
};
然后,在您的构造函数中:
Then, in your constructor:
OpeningBook::OpeningBook()
{
for (int i = 0; i < sizeof(DatabaseInitData)/sizeof(DatabaseInitData[0]); i++) {
opening_database_.insert(std::pair<std::string, int>(
DatabaseInitData[i].str,
DatabaseInitData[i].n));
}
}
这篇关于无序映射堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!