1. The C++ primitive new
is for allocating memory where?
2. Fill in the blank in this declaration and definition of a variable using new
:
_____________ b = new float[100];
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.
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).
Goals:
Reading: ODS section 2.1
We went over the basic structure of the implementation.
Look at ODS section 1.2 key data structure interfaces (especially 1.2.2)
... also look at file PrimitiveStack.h
(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:
Code: udods/ArrayStack.h -- let's understand every detail of this!