簡體   English   中英

我得到了我無法解決的錯誤:拋出'std :: bad_alloc'what()實例之后終止調用:std :: bad_alloc中止(核心已轉儲)

[英]I get the error which i cant fix : terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped)

我使用了2D動態數組,但我不知道如何解決該錯誤,請幫幫我! 我想從用戶那里得到一個字符串,並將其分離為一些字符串,然后將它們放入2d動態數組中。 它是我分配數組的代碼部分。

    int colCount,rowCount;
    string** table = new string*[rowCount];
    for(int i = 0; i < rowCount; ++i)
    {
    table[i] = new string[colCount];
    }

您的代碼不會初始化colCountrowCount ,因此它們的值是垃圾。 您嘗試使用未初始化的變量動態分配內存,這當然會調用Undefined Behavior

初始化變量,例如:

int colCount = 5, rowCount = 5;

PS:因為這是C ++,所以建議您將std::vector用作2D數組,例如:

std::vector<std::vector<std::string>> table;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM