找不到内存泄漏

时间:2012-02-16 22:16:15

标签: c++ arrays pointers memory-leaks

我正在学习C ++,我的讲师让我们重温动态内存分配。在下面的代码中,我似乎有内存泄漏,因为当我运行程序时,我必须在最后处理它。

在输出中,我得到的一切都是正确的,除了Data:字段应该列出数组中的所有数字,而是给我一些-3435893 jibberish。

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

1 个答案:

答案 0 :(得分:6)

我发现的第一件事就是在sortArray中您可以访问元素count + 1,它可以是数组末尾的一个元素。之后,关于你的程序行为的所有其他赌注都已关闭。