[英]Can't find memory leak
I am learning C++ and my instructor has us going over dynamic memory allocation. 我正在学习C ++,我的老师让我们研究了动态内存分配。 In the below code it seems I have a memory leak because when I run the program I must about it at the end. 在下面的代码中,似乎有内存泄漏,因为在运行程序时,我必须在结尾处进行处理。
In the output I get everything is correct except for the Data:
field which should list all the numbers in the array, but instead gives me some -3435893
jibberish. 在输出中,我得到的所有信息都是正确的,但Data:
字段应列出数组中的所有数字,但给了我一些-3435893
胡言乱语。
Lowest:
also does this. Lowest:
也这样做。 After the program displays everything I get a memory error that memory is being written to after end of heap buffer. 程序显示所有内容后,我收到一个内存错误,即在堆缓冲区结束后正在写入内存。
I'm new to all this but I'm guessing the problem is when arrPTR[0] is accessed just because I get the same problem for Lowest:
and not Highest:
. 我不熟悉所有这些,但是我想问题是访问arrPTR [0]时仅仅是因为我遇到了Lowest:
而不是Highest:
的相同问题。 I don't know for sure but I would appreciate any help I can get. 我不确定,但我将不胜感激。
#include <iostream>
using namespace std;
int* readArray(int&);
void sortArray(int *, const int * );
int findMode(int*, const int *);
double averageNumber(int*,const int*);
void lowestHighest(int*,const int*,int&,int&);
void printFunc(int*,const int*, int, int, double);
int main ()
{
int size = 0;
int *arrPTR = readArray(size);
const int *sizePTR = &size;
sortArray(arrPTR, sizePTR);
int mode = findMode(arrPTR,sizePTR);
double average = averageNumber(arrPTR, sizePTR);
int lowest = 0, highest = 0;
lowestHighest(arrPTR,sizePTR,lowest,highest);
printFunc(arrPTR,sizePTR,lowest,highest,average);
delete [] arrPTR;
system("pause");
return 0;
}
int* readArray(int &size)
{
cout<<"Enter a number for size of array.\n";
cin>>size;
int *arrPTR = new int[size];
for(int count = 0; count < size; count++)
{
cout<<"Enter positive numbers to completely fill the array.\n";
cin>>arrPTR[count];
}
return arrPTR;
}
void sortArray(int *arrPTR, const int *sizePTR)
{
int temp;
bool swap;
do
{
swap = false;
for(int count = 0; count < *sizePTR; count++)
{
if(arrPTR[count] > arrPTR[count+1])
{
temp = arrPTR[count];
arrPTR[count] = arrPTR[count+1];
arrPTR[count+1] = temp;
swap = true;
}
}
} while (swap);
}
int findMode(int *arrPTR, const int *sizePTR)
{
int most_found_element = arrPTR[0];
int most_found_element_count = 0;
int current_element = arrPTR[0];
int current_element_count = 0;
int count;
for (count = 0; count < *sizePTR; count++)
{
if(count == arrPTR[count])
current_element_count++;
else if(current_element_count > most_found_element)
{
most_found_element = current_element;
most_found_element_count = current_element_count;
}
current_element = count;
current_element_count=1;
}
return most_found_element;
}
double averageNumber(int *arrPTR,const int *sizePTR)
{
double total = 0;
for (int count = 0; count > *sizePTR; count++)
total+=arrPTR[count];
double average = total / *sizePTR;
return average;
}
void lowestHighest(int *arrPTR, const int *sizePTR,int &lowest, int &highest)
{
//Since array is already sorted the lowest number will be in the lowest element and the highest will be in the highest element.
lowest = arrPTR[0];
highest = arrPTR[*sizePTR-1];
}
void printFunc(int *arrPTR, const int *sizePTR, int lowest, int highest, double average)
{
cout<<"Array Stats\n";
cout<<"Data:";
for(int count = 0; count < *sizePTR; count++)
cout<<arrPTR[count];
cout<<"\n";
cout<<"Mode:"<<endl;
cout<<"Average:"<<average<<endl;
cout<<"Low Value:"<<lowest<<endl;
cout<<"High Value:"<<highest<<endl;
}
The first thing I spot is that in sortArray
you access element count + 1
which can be one-past the end of your array. 我发现的第一件事是,在sortArray
您可以访问元素count + 1
,这可以是数组末尾的最后一位。 After that, all other bets about your program behavior are off. 之后,关于程序行为的所有其他押注都将关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.