繁体   English   中英

找不到内存泄漏

[英]Can't find memory leak

我正在学习C ++,我的老师让我们研究了动态内存分配。 在下面的代码中,似乎有内存泄漏,因为在运行程序时,我必须在结尾处进行处理。

在输出中,我得到的所有信息都是正确的,但Data:字段应列出数组中的所有数字,但给了我一些-3435893胡言乱语。

Lowest:也这样做。 程序显示所有内容后,我收到一个内存错误,即在堆缓冲区结束后正在写入内存。

我不熟悉所有这些,但是我想问题是访问arrPTR [0]时仅仅是因为我遇到了Lowest:而不是Highest:的相同问题。 我不确定,但我将不胜感激。

#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;
}

我发现的第一件事是,在sortArray您可以访问元素count + 1 ,这可以是数组末尾的最后一位。 之后,关于程序行为的所有其他押注都将关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM