尝试向静态映射添加大量元素时出现段故障异常
Segment fault exception when try to add large number of elements to a static map
问题:尝试向映射中添加大量元素时出现段故障异常。
下面的程序运行得很好,但如果我给它添加大约80000个元素,它在运行时就会崩溃。从我在谷歌上搜索的情况来看,这是由于程序可用的堆栈大小有限。有什么变通办法吗?基本上,我只需要保存一张城市ID到城市名称的地图,大约有90000个城市。
实用程序.hpp
#ifndef Utilities_hpp
#define Utilities_hpp
#include <string>
#include <map>
class Utilities {
public:
typedef std::map<long, const char *> CityMap;
static CityMap cityIdNameMap;
public:
static std::string getCurrencySymbol(std::string &$currenyName);
static const char *getCityFromId(long cityId);
static CityMap createMap();
};
#endif /* Utilities_hpp */
实用程序.cpp
#include "Utilities.hpp"
Utilities::CityMap Utilities::cityIdNameMap = Utilities::createMap();
Utilities::CityMap Utilities::createMap() {
CityMap tmpMap = {
{1000010,"Abu Dhabi,Abu Dhabi,United Arab Emirates"},
{1000010,"Abu Dhabi,Abu Dhabi,United Arab Emirates"},
{1000011,"Ajman,Ajman,United Arab Emirates"}
};
return tmpMap;
}
// http://stackoverflow.com/questions/13464325/static-map-initialization
//class Utilities {
std::string Utilities::getCurrencySymbol(std::string &$currenyName) {
return "$";
}
const char *Utilities::getCityFromId(long cityId) {
return Utilities::cityIdNameMap[cityId];
}
main.cpp
#include <iostream>
#include <istream>
#include "Utilities.hpp"
using std::cout;
using std::endl;
int main(int argc, char *argv[])
{
cout << Utilities::getCityFromId(1000011);
return 0;
}
段故障日志:
System Integrity Protection: disabled
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_PROTECTION_FAILURE at 0x00007fff57bf8ff8
VM Regions Near 0x7fff57bf8ff8:
MALLOC_SMALL 00007fbf5c000000-00007fbf5c800000 [ 8192K] rw-/rwx SM=PRV
--> STACK GUARD 00007fff543f9000-00007fff57bf9000 [ 56.0M] ---/rwx SM=NUL stack guard for thread 0
Stack 00007fff57bf9000-00007fff583f9000 [ 8192K] rw-/rwx SM=COW thread 0
Application Specific Information:
/*********/CSVParser/DerivedData/Build/Products/Debug/./CSVParser
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 CSVParser 0x00000001079e53a3 Utilities::createMap() + 1955795 (Utilities.cpp:13)
1 CSVParser 0x0000000108da2384 __cxx_global_var_init + 20 (Utilities.cpp:11)
2 CSVParser 0x0000000108da23b9 _GLOBAL__sub_I_Utilities.cpp + 9
3 dyld 0x00007fff6bd6410b ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) + 265
4 dyld 0x00007fff6bd64284 ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) + 40
5 dyld 0x00007fff6bd608bd ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 305
6 dyld 0x00007fff6bd60743 ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) + 127
7 dyld 0x00007fff6bd609b3 ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) + 75
8 dyld 0x00007fff6bd530f1 dyld::initializeMainExecutable() + 208
9 dyld 0x00007fff6bd56d98 dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) + 3596
10 dyld 0x00007fff6bd52276 dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) + 512
11 dyld 0x00007fff6bd52036 _dyld_start + 54
映射是在堆中分配的,但我想发生的是初始化器列表是在堆栈上分配的,因此是segfault。
或者,您可以:
- 从文件中读取ID城市对,而不是对其进行硬编码
- 如果您需要createMap非常快的性能,您可以尝试boost::serialization方法将映射序列化为二进制文件并对其进行反序列化-确保测量它是否真的更快
相关文章:
- Python & C-C++ 扩展模块案例段故障
- 中断;导致段故障
- 带指针的调用函数会导致段故障
- 由于变量范围导致的段故障
- 为什么通过shared_from_this()会导致段故障
- 简单的C++字符数组加密功能 - 段故障
- std::vector的函数push_back中的堰段故障
- 在openCV中将cvPoint[][]转换为cvPoint**时出现段故障
- 模板变量分配段故障11
- CIN 一个非常大的 int 数组,段故障 11
- C++段故障错误
- 段故障 -> 在标准::basic_ostream<字符中,标准::char_traits<char>
- C++中的二叉树段故障
- 调试执行工作正常,但正常的一段故障
- c++:几乎相同的输入,但第二次调用显示段故障
- 段故障。地址0x0不是堆叠的、恶意的或(最近)释放的
- 使用矢量引起的段故障
- 使用arm-linux- gnuabi -g++ -o时出现段故障,不使用-o也不会出现问题
- 为什么会出现以下段故障
- 尝试向静态映射添加大量元素时出现段故障异常