【问题标题】:Hitting a segfault when copying local vector to the value of a certain key in map将本地向量复制到映射中某个键的值时遇到段错误
【发布时间】:2017-08-01 16:25:47
【问题描述】:
class TrackSymbol
{
        protected: static std::map<int, std::vector<char> > _trackTypeToIdentificationCodeMap ;

        protected: static char _identificationCodeChars[][2] ;
} ;

在实现文件中:

std::map<int, std::vector<char> > TrackSymbol::_trackTypeToIdentificationCodeMap ;

char TrackSymbol::_identificationCodeChars[][2] =
{
    { ' ', ' ' },
    { 'S', '6' },
    { 'Z', 'U' }
} ;

在构造函数中,我试图用 _identificationCodeChars 中的适当值填充本地向量,但在副本中遇到了段错误:

    for ( int i = 0 ; i < sizeof( _identificationCodeChars ) / sizeof( _identificationCodeChars[0] ) ; i++ )
    {
        std::vector<char> vec ;
        vec.push_back( _identificationCodeChars[i][0] ) ;
        vec.push_back( _identificationCodeChars[i][1] ) ;

        _trackTypeToIdentificationCodeMap[i] = vec ; //Segfault here
    }

我的理解是赋值运算符会对本地向量进行硬拷贝,所以我对它为什么会遇到分段错误感到有些困惑。另外,在我当前的环境中,我仅限于 C98,所以我无法使用扩展初始化列表等内容。

【问题讨论】:

标签: c++ vector stl segmentation-fault


【解决方案1】:

静态成员变量基本上是一个花哨的全局变量。特别是该地图的构造函数在该程序的执行中仅被调用一次,而不是为每个 TrackSymbol 对象调用一次。 (但是,它保证在int main()中执行第一行之前运行。)

从你所说的很难判断,但我猜你已经在_trackTypeToIdentificationCodeMap 的构造函数运行之前实例化了一个 TrackSymbol 对象,例如在_trackTypeToIdentificationCodeMap 之前定义的全局变量中或另一个文件。直接的解决方法是移动该定义。更好的解决方法可能是使该成员变量非静态(如果您能承受性能损失)或使用单例函数(在旧 C++ 版本中,这不能保证是线程安全的):

std::map<int, std::vector<char> > getCodeMap_internal()
{
    static char _identificationCodeChars[][2] =
    {
        { ' ', ' ' },
        { 'S', '6' },
        { 'Z', 'U' }
    } ;
    std::map<int, std::vector<char> > _trackTypeToIdentificationCodeMap;
    for ( int i = 0 ; i < sizeof( _identificationCodeChars ) / sizeof( _identificationCodeChars[0] ) ; i++ )
    {
        std::vector<char> vec ;
        vec.push_back( _identificationCodeChars[i][0] ) ;
        vec.push_back( _identificationCodeChars[i][1] ) ;

        _trackTypeToIdentificationCodeMap[i] = vec ;
    }
    return _trackTypeToIdentificationCodeMap;
}

const std::map<int, std::vector<char> >& getCodeMap()
{
    static std::map<int, std::vector<char> > _trackTypeToIdentificationCodeMap
        = getCodeMap_internal();
    return _trackTypeToIdentificationCodeMap;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2013-04-13
    • 2016-01-17
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    相关资源
    最近更新 更多