Resizing Arrays
![]()
Virtual Lecture
Dynamically Sized Arrays
Often times you don't know how big an array should be. For example, if we were creating a contact manager application, we may use an array to load the contact information from a file, and display it. So how many contacts do we allow? We could just make a very large array, say for 10,000 contacts. That would be a huge waste for most people who may only have fifty or so contacts. The answer is to have an array that can be resized as needed.
So how can we "grow" an array when needed?
If you create an array of ten integers, twenty bytes of memory are reserved for the array on the heap. Now, you go and create another array of doubles. It is likely that the array of doubles will have a starting address right at the end of your integer array. So, if you try to make your integer array bigger, you end up corrupting the double array.
The only way to make an array bigger is to make a new array that is larger than the original, then copy the contents of the original array to the new array. Pseudo code might look something like this:
int origArray [10];
int sizeOfArray = 10;
// Put some stuff in origArray. Oops, need bigger array.
int newArraySize = 30;
int newArray [newArraySize];
// Init new array.
for(i = 0; i < newArraySize; i++)
{ newArray[i] = 0; }
// Copy contents to new array.
for(i = 0; i < sizeOfArray; i++)
{ newArray[i] = origArray[i]; }
// Get rid of old array and clean up.
free(origArray);
sizeOfArray = newArraySize;
origArray = newArray;
This is a lot of work and very expensive on system resources. But this is what you have to do to resize an array, old school.
C# provides some advantages in that the array is an object that already keeps track of the size. You don't have to keep track of these things separately. Next, C# arrays provide a static method you can use to resize an array, called Resize. All of the operations described above are still necessary. However, the Resize method does all the work and makes resizing arrays easy. Just because resizing is easy, you still want to use caution. The resize of an array is still expensive. You want to minimize resizing.
Resize Method
The resize method is a static method in the Array class. So you don't call this method directly on your array. Instead, you call the static method and pass your array (or more correctly, the address of your array) to the method.
int[] newArray = new int [5];
// Put some stuff in the array.......
Array.Resize(ref newArray, 10);
newArray gets resized from 5 to 10. The original values in the array are preserved in the first five elements of the array. Since arrays are always reference types, the call to Array.Resize wants to see the ref keyword before the array name.
Here is an example of using Resize array in a method that adds a new element to the array. In our discussion so far, we have seen array examples using atomic types. Arrays can also hold complex objects. In this example, I have a Contact object. This object is from a class that holds information about a single contact like first name, last name, phone, etc. If I were writing a Contact Manager application, I could hold all of the contacts in an array and use this array to read / write contacts to disk, display a list, etc.
private void AddContact(CContact contact)
{
int arraySize = m_arContacts.GetLength(0);
if (arraySize == m_curIndex)
{
Array.Resize(ref m_arContacts,
arraySize +
INITIAL_NUM_ELEMENTS);
}
m_arContacts[m_curIndex] = contact;
m_curIndex++;
}
In this example, I have a class variable m_curIndex which keeps track of where the next index is that I'm going to put the new contact. Indexes are zero based, so if the next location (m_curIndex) is equal to the size of the array, I have a problem. I need to make the array bigger. In this case I chose to create a constant INITIAL_NUM_ELEMENTS. Anytime the array needs to grow bigger, I increase the size by this amount. Using a constant here is a good idea because I can adjust the "grow by" amount of the array by changing this single constant. This is rather simple code, but a good working example of how to grow your arrays dynaically.