Standards-7
Arrays, Pointers and Memory Allocation


Major errors

Do not go outside of either physical or logical array bounds [20 points each, 15% max]

Do not use an array index that is outside of the array bounds. If only a prefix or suffix of an array has relevant information in it, only look at the relevant information. For example, if you have created an array of physical size 100, but you have read information into the first 30 slots, then do not do a loop over all 100 slots just to see those 30 values.

Bad delete [20-40 points each, 8% max]

If you perform
  delete p;
then you must have obtained pointer p using new, for example by
  p = new T;
If you perform
  delete [] p;
then you must have obtained pointer p using new, for example by
  p = new T[n];

Do not delete something that you still need.

Do not delete the same thing twice.


Do not delete any pointer that was not given to you by new. [20 points each, 8% max]

Do not delete anything that you still need. If you perform
  delete p;
then you must have obtained pointer p using new, for example by
  p = new T;
If you perform
  delete [] p;
then you must have obtained pointer p using new, for example by
  p = new T[n];

If you delete an array, follow delete by [ ]. [4-8 points each, 2% max]

If you allocate an array using
  p = new T[n];
then delete that array using
  delete [] p;

Do not make use of a dangling pointer [20-40 points each, 8% max]

Do not delete something before you are done with it. That creates a dangling pointer.

Do not use a pointer variable that is uninitialized. It is considered a dangling pointer since you do not know what it points to.


Avoid memory faults during execution [7%-30%]

A memory fault occurs when a program uses an address that it does not own. The program stops immediately.

An array is too small [5-15 points each, 10% max]

Do not allocate an array that is smaller than you need. That will lead to an array bounds error, which is very serious.


Minor errors

Avoid memory leaks [3-8 points each, 3% max]

A memory-leak occurs when a program loses access to memory without deleting it.

Use a named constant for a fixed array size [4 points each, 3% max]

If you need to choose an arbitrary fixed array size, the size must be a named constant. For example, somewhere near the beginning of the program, define
  const int maxNumberOfWidgets = 200;
Then, when you create an array of widgets, say
  widget widgetArray[maxNumberOfWidgets];
Never write 200 where you should use maxNumberOfWidgets. The idea is that you should be able to change 200 to 500 in the definition of maxNumberOfWidgets and have the program work with the new constant, without changing the program in any other places.

Do not overwork a constant. If you need an array of bolts, do not make its size be maxNumberOfWidgets. Create another constant, such as maxNumberOfBolts.

Only use a constant array size when that seems necessary. If you know that you need an array of size n, then create an array of size n, not an array of size maxNumberOfWidgets.


An array is too large [3 points each, 2% max]

Do not allocate an array that is much larger than you need, unless you have a good reason for doing that. (If you allocate extra room because you expect the number of things stored in the array to grow, then that is a good reason.)

An array is unnecessarily passed by reference [2 point each, 1% max]

If you pass an array by value, it is really being passed by pointer. So you have a pointer to the array, and you can change the array.

Only pass an array by reference if you intend to make the pointer point to a different array.