After I discovered about C++ standard vectors, I've been using them ever since.
But just today a friend of mine, who is just learning C++, asked me about dynamically increasing the size of arrays.

Now, I happily went to code an example.
The program reads a file for a whitespace separated arbitrary length list of integers, saves it into an array, then prints the contents of the array. Simple enough.
Now, I discovered that for some reason, the first element in the array always became set to 0.
In particular, it's value is detected to be 0 just after the portion of code that enlarges the array.

In case I got the array expansion wrong, I checked with another friend, who duly provided me with a template function that pretty much did the same thing as I was doing.
Consequently, there was no change in result - the first element remained zeroed.

Here's is the source file in question:
Code:
#include <iostream>
#include <fstream>

using namespace std;

ifstream& readInput(ifstream& ifs, int arr[], int& size, int& max_size);

template<typename T> void enlarge(T* array, int& size) {
	int init_size = size;
	T* temp = new T[init_size];
	for (int i = 0; i < init_size; ++i)
		temp[i] = array[i];
	delete[] array;
	size += size;
	array = new T[size];
	for(int i = 0; i < init_size; ++i)
		array[i] = temp[i];
	delete[] temp;
	temp = 0;
}

int main() {
	int max_size = 10;
	int* arr = new int[max_size];
	int size = 0;
	ifstream ifs("test.txt");
	if (ifs.good())
		readInput(ifs, arr, size, max_size);
	ifs.close();
	for (int i = 0; i < size; i++)
		cout << arr[i] << endl;
	delete[] arr;
	arr = 0;

	cin.sync();
	cin.get();
	return 0;
}

ifstream& readInput(ifstream& ifs, int arr[], int& size, int& max_size) {
	while (ifs.good()) {
		int temp;
		if (ifs >> temp) {
			if (size >= max_size) {
				enlarge(arr, max_size);
			}
			arr[size++] = temp;
		}
	}
	ifs.clear();
	return ifs;
}
Here is the input file, named test.txt
Code:
11
52
6
7
8
2
3
5
7
2
33
4
The compiler I used is the MinGW port of GCC 3.4.2 (i.e. g++), under the Dev-C++ IDE (Windows XP, no SP2)
Running the program produces each integer on its own line, except that "11" is printed as "0".
My debugging tests show that the value of arr[0] changes to 0 just after enlarge(arr, max_size).
Within the template function itself I detected no anomaly.

Does anyone have any idea what is the problem, and how to fix it?
Thanks.