C++ 检测到堆损坏:在正常块之后(#126)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11569473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Heap Corruption Detected: after Normal block (#126)
提问by user1066524
I cannot for the life of me figure out why I am getting this Debug Error:
我一生都无法弄清楚为什么会出现此调试错误:
Heap Corruption Detected: after Normal block (#126) at 0x004cF6c0 CRT detected that the application wrote to memory after end of heap bugger.
检测到堆损坏:在 0x004cF6c0 处的正常块 (#126) 之后 CRT 检测到应用程序在堆错误结束后写入内存。
I understand that you need to free memory whenever you use new operator, which I did and I am still getting problems.
我知道每当您使用 new 运算符时都需要释放内存,我确实这样做了,但我仍然遇到问题。
for some reason the program doesn't end correctly in the recursive function. I debugged it and went through each line of code with breakpoints.
由于某种原因,程序没有在递归函数中正确结束。我调试了它并通过断点检查了每一行代码。
At the end of the if statement in countSum it somehow subtracts 1 from i and then reenters the if block.....which it is not supposed to do.
在 countSum 中的 if 语句结束时,它以某种方式从 i 中减去 1,然后重新进入 if 块......这是它不应该做的。
Why is this occurring?
为什么会出现这种情况?
/*this program calculates the sum of all the numbers in the array*/
#include <iostream>
#include <time.h>
using namespace std;
/*prototype*/
void countSum(int, int, int, int*);
int main(){
bool flag;
int num;
int sum = 0, j=0;
int *array = new int;
do{
system("cls");
cout<<"Enter a number into an array: ";
cin>>array[j];
cout<<"add another?(y/n):";
char choice;
cin>>choice;
choice == 'y' ? flag = true : flag = false;
j++;
}while(flag);
int size = j;
countSum(sum, 0, size, array);
//free memory
delete array;
array = 0;
return 0;
}
void countSum(int sum, int i, int size, int *array){
if (i < size){
system("cls");
cout<<"The sum is :"<<endl;
sum += array[i];
cout<<"...."<<sum;
time_t start_time, stop_time;
time(&start_time);
do
{
time(&stop_time); //pause for 5 seconds
}
while((stop_time - start_time) < 5);
countSum(sum, (i+1) , size, array); //call recursive function
}
}
回答by hmjd
array
holds enough space for a single int
:
array
有足够的空间容纳单个int
:
int *array = new int;
but there is potentially an attempt to insert more than one int
which would result in writing to memory that is not available. Either use a std::vector<int>
or it must be known beforehand the maximum number of int
s that will be entered before array
is allocated.
但是可能会尝试插入多个,int
这会导致写入不可用的内存。要么使用 astd::vector<int>
要么必须事先知道分配int
之前将输入的最大s数array
。
If this is a learning exercise and you do not want to use std::vector<int>
you could prompt the user to enter the number of int
s they wish to enter:
如果这是一个学习练习并且您不想使用,std::vector<int>
您可以提示用户输入int
他们希望输入的s的数量:
std::cout << "Enter number of integers to be entered: ";
int size = 0;
std::cin >> size;
if (size > 0)
{
array = new int[size];
}
Then accept size
number of int
s. Use delete[]
when you use new[]
.
然后接受s 的size
数量int
。使用delete[]
时使用new[]
。
回答by user1066524
The solution was to set the size new int[size]....although I wish that you didn't have to set a size if it is dynamic.
解决方案是设置大小 new int[size]....虽然我希望你不必设置大小,如果它是动态的。