尝试向静态映射添加大量元素时出现段故障异常

Segment fault exception when try to add large number of elements to a static map

本文关键字:段故障 异常 故障 元素 静态 映射 添加      更新时间:2023-10-16

问题:尝试向映射中添加大量元素时出现段故障异常。

下面的程序运行得很好,但如果我给它添加大约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。

或者,您可以:

  1. 从文件中读取ID城市对,而不是对其进行硬编码
  2. 如果您需要createMap非常快的性能,您可以尝试boost::serialization方法将映射序列化为二进制文件并对其进行反序列化-确保测量它是否真的更快