5.11.2 Array Size

Often, not all of the memory in an array is used. For example, you might have an array with 100 spots, but you are only currently using the first 10 of them. Think of the array as similar to a hotel. There are a given number of rooms available, but on a given night, not all of the rooms are occupied. Unlike a hotel, however, the occupied part is typically at the beginning, using indices 0, …, n−1 for some value n.

For arrays that are partially full, there are two notions of the size of the array.

  1. The physical size of the array is the total number of slots that are available. For example, if array A is created by

      double A[15];
    
    then A has physical size 15.

  2. The logical size of an array is the total number of occupied slots. It must be less than or equal to the physical size.

When you think about the size of an array, be sure to distinguish these two concepts of size. Sometimes, you are interested in the physical size. Often, you are interested in the logical size. For example, to show the entire content of an array, you would show up to the logical size, since you don't want to see the unoccupied part.