代码之家  ›  专栏  ›  技术社区  ›  Enigma Jim Hunziker

使用常量字符串数组崩溃初始化向量

  •  2
  • Enigma Jim Hunziker  · 技术社区  · 6 年前

    我有一个试图初始化 vector<string> 有9000个字符串,程序在类构造函数的入口点崩溃/中断。

    这是我能看到的唯一断点信息(我无法理解):

    ; Find next lower page and probe
    cs20:
            sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
            test    dword ptr [eax],eax     ; probe page. // < breaks here
            jmp     short cs10
    
    _chkstk endp
    

    如果我用更小的字符串数组运行相同的代码 {"string1", "string2"} ,类构造函数的入口没有崩溃。


    someclass.cpp:某个类别:

    #include "someclass.h"
    
    using namespace std;
    
    CSomeClass::CSomeClass()
    {   // < crashes/breaks right here or between here and next instruction
    
        // Crashes
        m_vStrings = {"string1", "string2", ... "string9000"};
    
        // Works
        m_vStrings = {"string1", "string2"};
    }
    

    某个班级。h:

    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class CSomeClass    
    {
    protected:
        vector<string>                      m_vStrings;
    
    public:
        CSomeClass();
    };
    

    似乎所有的东西都是按功能顺序排列的,但是我可能会遇到某种,我敢说,堆栈溢出?

    用法:

    CSomeClass class1; // crashes in here so usage hasn't been tested yet
    

    这很有效,但我希望尽可能避免双重分配:

    const char* sStrings[] = {"string1", "string2", ... "string9000"};
    m_vStrings = vector<string>(sStrings, end(sStrings));
    
    0 回复  |  直到 6 年前
    推荐文章