Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When deleting array: "Process returned -1073740940 (0xC0000374)" Only with certain numbers

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);

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading