The program returns the user N number of odd squares starting from 1.
From numbers 5-10 and then 20, (I didn't go further) when deleting array A it crashes with the error message: "Process returned -1073740940 (0xC0000374)". Which is apparently a memory violation?
#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);
}
CodePudding user response:
From the NTSTATUS reference:
0xC0000374 STATUS_HEAP_CORRUPTION - A heap has been corrupted
You appear to access A
(a heap allocated object) out of bounds - A[0]
through A[size-1]
are valid elements to access but counter
goes as high as 2*size
. Any attempts to write to values past A[size-1]
can corrupt the heap leading to this error.
Calculate counter
first and use that as the allocation size.