C++ 我得到了一个无法修复的错误:在抛出';std::bad#u alloc';what():std::bad_ALOC中止(内核转储)

C++ 我得到了一个无法修复的错误:在抛出';std::bad#u alloc';what():std::bad_ALOC中止(内核转储),c++,arrays,memory-management,dynamic-memory-allocation,bad-alloc,C++,Arrays,Memory Management,Dynamic Memory Allocation,Bad Alloc,我使用了2D动态数组,我不知道如何修复错误,请帮助我!我想从用户那里得到一个字符串,把它分离成一些字符串,然后把它们放到2d动态数组中。 这是我分配数组的代码部分 int colCount,rowCount; string** table = new string*[rowCount]; for(int i = 0; i < rowCount; ++i) { table[i] = new string[colCount]; } int co

我使用了2D动态数组,我不知道如何修复错误,请帮助我!我想从用户那里得到一个字符串,把它分离成一些字符串,然后把它们放到2d动态数组中。 这是我分配数组的代码部分

    int colCount,rowCount;
    string** table = new string*[rowCount];
    for(int i = 0; i < rowCount; ++i)
    {
    table[i] = new string[colCount];
    }
int colCount,rowCount;
字符串**表=新字符串*[rowCount];
对于(int i=0;i
您的代码没有初始化
colCount
rowCount
,因此它们的值是垃圾。您尝试使用未初始化的变量动态分配内存,当然,这会调用未定义的行为

初始化变量,如:

int colCount = 5, rowCount = 5;

ps:因为这是C++,所以我建议你使用2D数组,比如这个例子:

std::vector<std::vector<std::string>> table;
std::向量表;

首先,您可能应该使用
std::vector
。您可以将一个向量嵌套在另一个向量中。至于您的问题,请尝试创建一个向量并向我们展示。例如,您需要告诉我们
colCount
rowCount
的值,最好告诉我们如何初始化它们。还有,请花点时间。我希望类型是字符串,我可以使用字符串的2D向量来模拟一个种类数据库吗?2D向量给你一个矩阵@shirazy,其中
table[I][j]
是一个字符串。是的!对不起,我不明白。我想要一个二维的字符串向量,可以吗?像这样的向量或者使用2D动态数组会更好?@shirazy是的,对不起!检查我的更新答案,如果有帮助,请接受!=)