即使在调整2D矢量大小后,也会抛出std bad_alloc

时间:2017-04-07 22:48:15

标签: c++ class vector bad-alloc

我有以下代码:

struct elements
{
    char status;
    int value;
};
class matrix
{
    vector<vector <elements> > mat;
    int varNum;
    int ConNum;
public:
    void resetmatrix(int varnum, int connum)
    {
        varNum = varnum;
        ConNum = connum;
        mat.resize(connum, vector<elements>(varnum) );
    }
    matrix(int x, int y)
    {
        varNum = 0;
        ConNum = 0;
    }
};
matrix mat(0,0);

int main(int argc, char *argv[])      //main function
{
    int variables,cubes;
    variables=25;
    cubes= 10000000;
    cout<<"Variables= "<<variables<<endl<<"Cubes= "<<cubes<<endl;
    mat.resetmatrix(variables, cubes);
    cout<<"hello";
    return 0;
}

我收到以下错误

enter image description here

我认为只有当您没有调整矢量大小时才会出现此类错误。但我正在调整它。谁能告诉我为什么会这样?是因为立方体的价值非常高(1000万)?

0 个答案:

没有答案