HEAP DETECTION ERROR--having trouble debugging

I am trying to write a program that will take integers and find the mean using a dynamic array that expands as more integers are entered. I think I have everything right so far and the program builds fine but after entering 6 integers, the program crashes and gives me the HEAP CORRUPTION DETECTED error. I've spent many hours googling and trying to understand the error but I cant figure out why its happening in my code. Any help would be appreciated. Thanks!

Here's what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>
using namespace std;

//Declare the appropriate functions...

int* FillList(int*, int&, int&);
int Totalizer(int*, int);
int* ExpandArray(int*, int, int&);
int FindAverage(int, int);
void DisplayAverage(int);
void ShowList(int*,int);

//Define the size of the DefaultSize
 int DefaultSize = 3;

int main()
{
   int* List = new int[DefaultSize];
   int Size, MaxSize, Total, Mean;
   MaxSize = DefaultSize;
   int K = 0;

   List = FillList(List, Size, MaxSize);
   Total = Totalizer(List, Size);
   cout << Total << endl;
   Mean  = FindAverage(Total, Size);
   ShowList(List, Size);
   DisplayAverage(Mean);
}

int* FillList(int* List, int& Size, int& MaxSize)
{
	string IValue;
	
	cout << "Enter Numbers Below, Stop To Quit" << endl;
	for(Size = 0 ; Size < MaxSize ; Size++) {
		cout << "Enter Value: ";
		getline(cin, IValue);
		if(IValue == "Stop")
			break;
		List[Size] = atoi(IValue.c_str()); 
		if(Size == MaxSize - 1)
			List = ExpandArray(List, Size, MaxSize);
	}
	return List;
}

int* ExpandArray(int* List, int Size, int& MaxSize)

{
	
   int* NewList = new int[(Size + DefaultSize)];

   for(int K = 0 ; K < Size ; K++)  
   {
	   NewList[K] = List[K];
	   cout << "NewList[ " << K << "] =" << NewList[K] << endl;
	 
   }
   MaxSize += DefaultSize;

   cout << "You may continue entering values..." << endl;

   delete[] List;
   List = NewList;

   return NewList;
   

}


		
int Totalizer(int* List, int Size)
{
   int Total = 0;

   for(int K = 0 ; K < Size ; K++)
	   Total += List[K];

   return Total;

}



int FindAverage(int Total, int Size)
{
	if(Size > 0) 
	  return Total / Size;
   return 0;

}


void DisplayAverage(int Mean)
{
	cout << "============================================" << endl;
	cout << "Mean Value = " << Mean << endl;
	cout << "============================================" << endl;
}

void ShowList(int* List, int Size)
{
	for(int K = 0 ; K < Size ; K++)
		cout << K << " --->" << List[K] << endl;
}
In line 55 make the comparison K <= Size.

Comment out lines 65 and 66 ( I don't know if this will cause a memory leak, VC++ didn't throw an error )

Then after line 29 place this:
delete[] List;

That fixed it! Thanks so much! Now I understand why there was a memory leak.
Topic archived. No new replies allowed.