程式回傳用戶 N 個從 1 開始的奇數平方。
從數字 5-10 到 20,(我沒有更進一步)洗掉陣列 A 時,它崩潰并顯示錯誤訊息:“行程回傳 -1073740940 (0xC0000374)”。哪個顯然是記憶體違規?
#include <iostream>
using namespace std;
int main(){
int ok;
int counter;
do {
int size;
while (true) {
cout << "Enter the number of perfect odd squares you want" << endl;
cin >> size;
if(size<1) {
cout << "Enter a valid number" << endl;
continue;
}
else break;
}
if (size%2==0) counter=size*2-1;
else counter=size*2;
int *A = new int[size];
for (int i=1; i<=counter; i=i 2){
A[i]=i*i;
cout<<A[i] << endl;
}
delete[]A;
cout << " Continue (1) or quit (0)?" << endl;
cin >> ok;
}while(ok==1);
}
uj5u.com熱心網友回復:
從NTSTATUS 參考:
0xC0000374 STATUS_HEAP_CORRUPTION - 堆已損壞
您似乎訪問A(堆分配的物件)越界 - A[0]throughA[size-1]是要訪問的有效元素,但counter最高可達2*size. 任何寫入過去值的嘗試A[size-1]都可能破壞導致此錯誤的堆。
counter首先計算并將其用作分配大小。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/366748.html
