我覺得我錯過了一些明顯的東西,我無法弄清楚。 基本上,似乎信息正確存儲在第一個for循環。但是當我在第二個循環打印出它的唯一垃圾值時。我錯過了什麼?我是比較新的這個用於陣列的C++動態內存分配
bignum::bignum(const string &digits)
{
int length = digits.length();
ndigits = 0;
for (int i = 0; i < length; i++)
{
if(isdigit(digits[i]))
{
ndigits++;
digit = new int[ndigits];
int tmpInt = digits[i] - '0';
digit[i] = tmpInt;
}
if(isalpha(digits[i]))
{
break;
}
cout <<"step "<< i << " " << digit[i] << endl;
}
for (int i = 0; i < ndigits; i++)
{
cout << digit[i] << " ";
}
cout << endl;
cout << "digits" << ndigits << endl;
cout << endl;
}
你正在像瘋了一樣泄漏內存。 – chris
@chris我有一個數字陣列的析構函數 – Chase
沒關係,你根據條件在循環內調用'new'。我看不出每個'new []'調用如何調用delete []'。 – juanchopanza