简体   繁体   中英

Can't find memory leak

I am learning C++ and my instructor has us going over dynamic memory allocation. 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.

Lowest: also does this. 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: . 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. After that, all other bets about your program behavior are off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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