删除数组时:“进程返回-1073740940 (0xC0000374)”仅针对特定数字
该程序返回用户从 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 NTSTATUS 参考:
您似乎越界访问
A
(堆分配的对象) -A[0]
到A[size-1 ]
是可访问的有效元素,但counter
高达2*size
。任何尝试写入超过A[size-1]
的值都可能会损坏堆,从而导致此错误。首先计算
counter
并将其用作分配大小。From the NTSTATUS reference:
You appear to access
A
(a heap allocated object) out of bounds -A[0]
throughA[size-1]
are valid elements to access butcounter
goes as high as2*size
. Any attempts to write to values pastA[size-1]
can corrupt the heap leading to this error.Calculate
counter
first and use that as the allocation size.