删除数组时:进程仅返回某些数字-1073740940(0xC0000374)&Quot; [英] When deleting array: "Process returned -1073740940 (0xC0000374)" Only with certain numbers

查看:0
本文介绍了删除数组时:进程仅返回某些数字-1073740940(0xC0000374)&Quot;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序从1开始向用户返回N个奇数平方。

从数字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);

}

推荐答案

来自NTSTATUS reference

0xC0000374 STATUS_HEAP_CROPERATION-A堆已损坏

您似乎访问A(堆分配的对象)超出了界限-A[0]A[size-1]是可以访问的有效元素,但counter最高可达2*size。任何写入超过A[size-1]的值的尝试都会损坏堆,从而导致此错误。

首先计算counter并将其用作分配大小。

这篇关于删除数组时:进程仅返回某些数字-1073740940(0xC0000374)&Quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆