2012-02-16 10 views
2

私はC++を勉強しています。私のインストラクターは動的メモリ割り当てを行っています。以下のコードでは、私がプログラムを実行するときに私は最後にそれをしなければならないので、私はメモリリークを持っているようです。メモリリークが見つかりません

出力には、Data:フィールドを除いて、すべての値が正しいことがわかります。このフィールドは、配列内のすべての数値をリストするはずですが、代わりに-3435893ジベルビッシュを示します。

Lowest:もこれを行います。プログラムがすべてを表示した後、ヒープバッファの終わりの後にメモリが書き込まれているというメモリエラーが発生します。

私はこのすべてに慣れていますが、Lowest:ではなく、Highest:で同じ問題が発生したためarrPTR [0]にアクセスすると問題が発生すると思います。私は確かにわからないが、私が得ることができるすべての助けに感謝します。

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

あなたが 'new []'したものを 'delete []'するので、メモリリークはありません。また、メモリーリークが原因で、些細なプログラムで誤った出力が生成されることはめったにないため、問題は他の場所にあります。 –

+1

これはC++よりメモリ管理の点で私にはよく似ています。 –

+0

intを渡す理由は何ですか?intを渡すのはなぜですか? –

答えて

6

私はスポット最初の事は一過去のあなたの配列の終わりすることができsortArrayであなたのアクセス素子count + 1ということです。その後、プログラムの振る舞いに関する他のすべての賭けはオフになります。

+0

+1私は前回その1つを見つけました;-)私はこれまでのすべての問題に十分な注意を払わなかったかもしれません。 – AJG85

+0

'for(int count = 0; count> * sizePTR; count ++)'も間違っています。 – MRAB

関連する問題