22B. Creating Arrays


Creating an array in the run-time stack

Declaration

  int A[10];
creates a chunk of 10 integers within the current function's frame and defines A to be a pointer to that chunk. Similarly,
  double stuff[100];
creates a chunk of 100 variables of type double in the current function's frame and makes stuff  point to it.

(You cannot change a pointer that is declared as an array.

  int A[10], B[10];
  A = B;
is not allowed.)


Allocating a chunk in the heap

If you want to use an array after the function that created it returns, allocate the array's chunk in the heap, not in the run-time stack. Expression new T[size] allocates a new chunk of size variables, each of type T. The value of expression new T[size] is the memory address of the first variable in that chunk, and its type is T*. For example, expression new int[25] has type int*, and

  int* A = new int[25];
allocates a new chunk of 25 ints and stores a pointer to the first one of those ints into variable A.

Watch out

To allocate an array, use square brackets around the size. If you accidentally use parentheses, as in new int(25), you will not get a compile error. Instead, the program allocates one variable and stores 25 in it. That is not a good language feature, but we have to live with it and watch out for it.


Fixed chunk sizes should be named constants

When you allocate a chunk, the size can be given by any expression whose value is a nonnegative integer. (Yes, you can have a chunk of size 0.) If you know exactly how much room you need, then allocate exactly that amount of room.

Occasionally, you need to create an array (and allocate a chunk for it) before you know how large you need it to be. That often occurs when a program reads data into an array, where you don't know in advance how much data there will be. Any time you need to allocate a chunk of a fixed size, make the size be a named constant. For example, suppose that you intend to read data into an array called data  whose variables have type DataType. Someplace where it is readily visible, such as near the beginning of a program, define a global constant.

  const int maxDataItems = 200;
When you create the array, make its chunk size be maxDataItems , as follows.
  DataType data[maxDataItems];

Then, to change the maximum number of data items allowed, you only need to change one line of the program. That only works, of course, if you always refer to the size of data  as maxDataItems , never as 200. The standards for this course require that.

Don't overuse a particular named constant. Each separate concept should have a separate constant, so that each of them can be changed independently.


Do not use constant array sizes unnecessarily

If it is easy to compute how large an array needs to be, then do that. Do not use a fixed size unnecessarily. That will cause your program to use either too much or (much worse) too little memory for the array. When you do compute an appropriate array size, it is crucial that you get it right!


Exercises

  1. Write a statement that allocates a new array of 75 characters called frog. The array's chunk should be in the heap. Answer

  2. What is the type of expression new long[12]? Answer

  3. There is something wrong with the following function. Explain what is wrong.

      // makeZeroedArray(n) returns an array with
      // n integers, all set to 0.
    
      int* makeZeroedArray(int n)
      {
        int* p = new int[n];
        for(int i = 0; i <= n; i++)
        {
           p[i] = 0;
        }
        return p;
      }
    
    Answer