You're using a pointer to point at 2 different addresses separately. |
So do you too. Different details, but same scenario.
Lets pretend that there are two values in the file. Lets "unroll" the loop:
int * array = new int [1]; // Address 1
// first iteration
fin >> array[0];
array = new int[2]; // Address 2
// second iteration
fin >> array[1];
array = new int[3]; // Address 3[/code]
Each call to
new
returns an address of different block of memory.
The code above allocates three blocks -- three separate arrays: for one, two and three integers.
Storing a new address to "array" overwrites the previous address. The previous block is still allocated, but nothing stores the address and it cannot be deallocated any more. Your
delete []
deallocates the last allocated block that you still have an address for.