![](/img/trans.png)
[英]How I can fix this error? terminate called after throwing an instance of '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];
}
您的代碼不會初始化colCount
和rowCount
,因此它們的值是垃圾。 您嘗試使用未初始化的變量動態分配內存,這當然會調用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.