[英]Debug Assertion failed c++
我正在做作業。 目的是實現用於對大整數( BigInt
)進行操作的類。
初始化:BigInt類的每個對象都構造為一個動態數組。 數組的成員是用戶輸入的字符串的數字(以相反的順序-這種方式對我來說更容易實現操作)。
BigInt類的規范:兩個BigInts(較小,較大,相等)之間的關系,運算:加法,減法,乘法。 關系,加法和減法效果很好。 問題在於乘法。
問題:當我不帶乘法測試程序時,它運行良好。 當我取消注釋乘法時,程序在寫入關系,加法和減法的結果后會中斷。
錯誤:調試斷言失敗。 當我進入“調試”模式時,我看到它的計算效果很好,直到該行為止:
DELETE [] m;
我查看了有關調試斷言的其他問題,並意識到我無法使用delete[] m
BigInt
類的對象,因為我已經在析構函數中編寫了該對象。 我嘗試了DELETE m
。 當我這樣做時,它會打印出良好的結果,但是在下一行之后會中斷:
cout << "before returing m" << endl;.
錯誤:調試斷言再次失敗,但是這次添加了:
L緩沖區太小。
當我進入調試模式時,它顯示斷點在Deep Copy構造函數中,行:
strcpy_s(myDigits, myNumDigits, source.myDigits);
現在我不知道該怎么辦,因為它可以很好地進行加減運算(但是我猜這些操作中從未使用過深拷貝構造函數)。
因此,我需要回答的問題是:哪里有錯誤,是使用delete[]
, delete
還是在深度復制構造函數的實現中(或其他地方)?
我正在附上我的代碼。 我不希望它很難復習,因此我將僅附上解決我的問題所需的部分(帶有構造函數,析構函數,乘法的代碼部分)。 如果您認為我應該附加更多內容,請在評論中告訴我。
施工/銷毀:
BigInt::BigInt(string digits)
{
const char* s = digits.c_str();
int k;
int limit = 0;
myNumDigits = 0;
if (strlen(s) == 0)
{
mySign = positive;
//myDigits = new char[2];
myDigits = nullptr;
myNumDigits = 0;
cout << "Empty string!" << endl;
return;
}
if (digits[0] == '-')
{
mySign = negative;
limit = 1;
}
else
{
mySign = positive;
if (digits[0] == '+')
limit = 1;
}
const char*help = digits.c_str();
myDigits = new char[strlen(s) + 1 - limit];
// reversed order
for (k = strlen(help) - 1; k >= limit; k--)
{
if (!isdigit(digits[k]))
{
cout << "onlu digits and sign! " << endl;
exit(1);
}
AddSigDigit(digits[k] - '0'); //incrementation
}
myDigits[strlen(s) - limit] = '\0';
}
// Copy constructor -deep
BigInt::BigInt(const BigInt& source)
{
// can do shallow copy of a number
myNumDigits = source.myNumDigits;
//deep copy of array
if (source.myDigits)
{
myDigits = new char[myNumDigits + 1];
strcpy_s(myDigits, myNumDigits, source.myDigits); //BREAKPOINT!!!!!
myDigits[myNumDigits] = '\0';
}
else
myDigits = 0;
}
// move copy constructor
BigInt::BigInt(BigInt&&other) {
myNumDigits = other.myNumDigits;
myDigits = other.myDigits;
other.myNumDigits = 0;
other.myDigits = 0;//null pointer
}
BigInt::BigInt(Sign sign, int NumDig, char* sum) {
mySign = sign;
myNumDigits = NumDig;
myDigits = sum;
sum = 0;
}
BigInt::~BigInt() {
delete[] myDigits;
}
幫助功能:
int BigInt::GetDigit(int k) const
{
if (0 <= k && k < NumDigits())
{
return myDigits[k] - '0';
}
return 0;
}
void BigInt::AddSigDigit(int value)
// adding value on place of most significant digit
{
myDigits[myNumDigits] = '0' + value;
myNumDigits++;
}
乘法:
BigInt BigInt::mul(const BigInt&num)const {
char* a =new char[num.myNumDigits+1];
strcpy(a, num.myDigits);
a[num.myNumDigits] = '\0';
cout << "a" << a << endl;
char* b = new char[myNumDigits + 1];
strcpy(b,myDigits);
b[myNumDigits] = '\0';
cout << "b" << b << endl;
string temp;
BigInt* m = new BigInt("0");
//cout << *m << endl;
unsigned int i;
for ( i = 0; i < strlen(a); i++) {
int carry = 0;
temp = string (i, '0');
unsigned int j;
for (j = 0; j < (int)strlen(b); j++) {
int pom;
pom = (a[i] - '0') * (b[j] - '0') + carry;
temp += ((char)(pom % 10 + '0'));
carry = pom / 10;
}
if (carry != 0)
temp += (carry + '0');
cout <<"temp unreversed in i. iteration" <<temp<<" " <<i << endl;
reverse(temp.begin(), temp.end());
cout << "temp reversed in i. iteration" << temp << " " << i << endl;
BigInt* n = new BigInt((*m).add(BigInt(temp.substr(cut(temp)))));
std::cout << "n in i. iteration " << *n<<"i."<<i<<endl;
delete m;
std::cout << " delete pass" << endl;
m = n;
n = nullptr;
}
cout << "before returing m" << endl;
return (*m); //BECAUSE OF THIS LINE THERE IS BREAKPOINT IN COPY CONSTRUCTOR
cout << "after returing m" << endl;
}
int BigInt::cut(const string& i)const {
int index = 0;
while (i[index] == '0' && index < i.size() - 1) index++;
return index;
}
mul
代碼創建一次m
,然后在循環中將其delete
。 歡迎來到UB。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.