저는 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;
}
당신은'new []'에'delete []'을했기 때문에 메모리 누출이 없습니다. 또한 메모리 누수로 인해 사소한 프로그램에서 잘못된 결과가 나오는 경우는 드물기 때문에 문제가 다른 곳에서 발생합니다. –
이것은 메모리 관리면에서 C++보다 C++과 비슷합니다. –
int를 전달할 때 포인터를 전달하는 이유는 무엇입니까? –