Review questions

1. The C++ primitive new is for allocating memory where?

  1. on the call stack
  2. on the free store
  3. on one of those areas, depends on context.
  4. none of the above
















2. Fill in the blank in this declaration and definition of a variable using new:
_____________ b = new float[100];

  1. float
  2. float []
  3. float *
  4. const float *
  5. float * const

















Notes for Week 2 lectures 4, 5, 6
on dynamic array based implementations of stacks and queues.

Discussion of pointers and arrays.
The pointer concept generalizes the array concept. Arrays are pointers that can't be changed, although the things they point to can be changed. Thus
T A[100]; // array declaration. A points to the first of 100 T's in memory. // ...in a call stack frame.
T *p; // pointer declaration. p points to nothing (yet).
p = new T[100]; // p points to 100 T's allocated in the free store.
p = A; // valid: p and A are now the same.
// ...but the allocation in free store is abandoned, a memory leak.
A = p; // invalid: A cannot be changed.
p = A + 50; // valid: p can be changed to point to A's 51st element.
T x = *p; // x is made a copy of what p points to.
T y = p[0]; // same as y = *p.
T z = p[-1] // same as z = *(p-1) and same as z = A[49].

Lecture 4.
Look at ODS section 1.2 key data structure interfaces (especially 1.2.2)

simple add/remove interfaces: stack(LIFO), queue(FIFO), priority queue(HPFO - I just made this acronym up).

More general interfaces List, USet, SSet


Lecture 5, Stacks

A use case: matching parentheses - look at files stack-example.cpp, checkMatch1.h (and 2.h).
... also look at file PrimitiveStack.h

Goals:
(a) constant time, O(1), for each operation.
(b) minimize wasted memory (the stack data structure should not be too much larger at any given moment than the number of items in the stack.


The ArrayStack implementation:

Reading: ODS section 2.1
Code: udods/ArrayStack.h -- let's understand every detail of this!

We went over the basic structure of the implementation.

Lecture 6, Stacks in a dynamic array.

Idea: add and remove at the high end.
Key issue: What if you run out of array (failure!) or if the array is large but you only have a few items in the stack (memory wasted).

Solution: periodically resize the array

Policy:

  1. When you are adding a new item (bringing the number of items to n) and the ArrayStack is full (length = n-1), allocate a new array of length 2n, copy the existing n-1 items there and add your new item.
  2. When you are removing an item (bringing the number of items to n) and the ArrayStack is too large (length = 3n), allocate a new array of length 2n, copy the first n of the existing n+1 items there and remove the last.

Illustrations:

Sequence of only adds adds:
     n: 0 1 2 3 4 5 6 7 8  9 10 11 12 13 14 15 16 17 18 19
length: 0 1 2 4 4 8 8 8 8 16 16 16 16 16 16 16 16 32 32 32

Sequence of only removes:
     n: 82 81 80 79 ... 28 27 26 25 24 ... 19 18 17 16 ... 12 11 10  9  8  7  6  5  4 3 2 1 0
length: 82 82 82 82 ... 82 82 54 54 54 ... 54 54 36 36 ... 36 24 24 24 24 16 16 12 12 8 6 4 2

In practice, of course the stack does not experience only adds or only removes. Rather it is an unpredictable mix of adds and removes. ...but we've already seen the worst case!

Clicker questions on ArrayStack

1. If resize() is called when n = length = 100, what are the resulting values of n and length?

  1. n = 100, length = 200
  2. n = 200, length = 100
  3. n = 200, length = 200
  4. n = 100, length = 300
















2. If resize() is called when n = length = 100, how many items are copied?

  1. 50
  2. 100
  3. 150
  4. 200
















3. If n = 8 and length = 12, how many removes would it take to cause a resize downwards?

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

4. If n = 8 and length = 12, how many removes would it take to cause a resize downwards?

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

5. If n = 8 and length = 12, and removals occur to cause a reduction in length, what is the new length?

  1. 12
  2. 10
  3. 8
  4. 6
  5. 4