Dynamically resizing arrays

Sometimes we discover partway through execution that an array is not big enough for our purposes.

One solution is to create a new (larger) array, copy the data from the original into the new one, and delete the old one.

For this to work, of course, the original must have been dynamically allocated as well.

The program below allows the user to keep entering data values until they enter a zero (hence we have no idea how many values they will ultimately enter).

It will store each of the values in a dynamically allocated array, with the array size starting at 100. Whenever the array turns out to be too small it will be replaced with a new one that is twice as big, all the old values will be copied across, and the old array will be deallocated.

#include <iostream>
using namespace std;

// try to allocate a new array that is twice
//     the size of the original
// if successful copy the contents across,
//    delete the old array, and return true
// otherwise return false 
//    (without altering arr or size)
bool resize(float* &arr, long &arrSize);

// default starting array size
const long DefaultSize = 4;

int main()
{
   // allocate the original array
   long size = DefaultSize;
   float *array = new float[size];
   // bail out if the original allocation fails
   if (!array) {
      cout << "Unable to allocate any storage" << endl;
      return 0;
   }

   // keep reading/storing values from the user
   //    until they enter a zero
   // resizing the array as needed
   cout << "Enter as many non-zero numeric values as desired,\n";
   cout << "followed by a zero to quit" << endl;
   float value;
   long position = 0;
   do {
      cin >> value;
      if (value != 0) {
         if (position >= size) {
            if (resize(array, size)) {
               cout << "Doubled array size to " << size << endl;
            } else {
               cout << "Unable to create enough array storage,\n";
               cout << "terminating input" << endl;
               break;
            }
         }
         array[position] = value;
         position++;
      }
   } while (value != 0);

   // print all the values out
   cout << "Contents\n";
   for (int i = 0; i < position; i++) {
       cout << array[i] << endl;
   }
   
   // deallocate the array and shut down
   delete [] array;
   cout << "Bye!" << endl;
   return 0;
}

// try to allocate a new array that is twice
//     the size of the original
// if successful copy the contents across,
//    delete the old array, and return true
// otherwise return false 
//    (without altering arr or size)
bool resize(float* &arr, long &arrSize)
{
   // make sure there *is* actually an original array
   if (!arr) return false;

   // allocate the new array
   long nsize = 2 * arrSize;
   float *n = new float[nsize];
   
   // bail out if unsuccessful
   if (!n) return false;

   // otherwise copy the data across
   for (int i = 0; i < arrSize; i++) {
       n[i] = arr[i];
   }

   // delete the old array
   delete [] arr;

   // update the parameters for the new array and size
   arrSize = nsize;
   arr = n;

   // return success
   return true;
}