Well, sorry for bringing up an old post... I just got a little too obsessed with it.
Laserlight's code are pretty much fine I think.
It's okay to change the value of array, as long as you doesent try to give value to something which has been deleted, without calling new first.
An example:
Code:
int *array = new int[MAX_SIZE];
delete[] array;
array = new int[MAX_SIZE];
*and use it however you'd like*
This is entirely legal, and arent the problem.
Also, you can call delete on the same pointer-variable, as long as it's either 0, or a new has been called first.
Laserlight does this correctly, and arent the problem either.
The correct solution for this problem is change
Code:
ifstream& readInput(ifstream& ifs, int arr[], int& size, int& max_size);
template<typename T> void enlarge(T* array, int& size);
to this:
Code:
ifstream& readInput(ifstream& ifs, int *&arr, int& size, int& max_size);
template<typename T> void enlarge(T *&array, int& size);
In short: when you use "new" inside functions and wanna send the pointer out through references, you gotta write:
Code:
type *&variableName;
This reads: "variableName are a refference to a pointer to type"
If you do this, your original code works fine, and no need to use the delete [] by adress