CIS 22B - Notes for Tuesday, 1/30

  Announcements and Reminders

  Pointers Continued

  Pointer Arithmetic
  • With one-dimensional arrays
  • With two-dimensional arrays

  A pointer to pointer

  Other stuff 

Return by address 

pointer to const, const pointer

  Dynamic Memory Allocation

  Three types of memory in the computer

  • Stack - for automatic variables, function arguments, and function return values
  • Static - for global contants, and static variables
  • Heap (aka free store) - for dynamic memory allocation

  The new and delete operators

  • new allocates memory
  • delete releases memory

int *ptr;
ptr = new int;
*ptr = 6;


int* ptr = new int(6);

Where is ptr stored?  Where is the dynamic memory?

Release the memory

delete ptr;

More examples:

float* ptrPI = new float(3.14);
delete ptrPI;

char* pc = new char;
*pc = 'x';
delete pc;

dog* pSirius = new dog;
delete pSirius;

Allocate memory for an array

int * pA = new int[5];
for (int i = 0; i < 5; i++)
     pA[i] = 5 * i;
delete [] pA;

char* pName = new char[6];
cout << *pName;
delete [] pName;

  • When you allocate memory dynamically, you should release when you are done with it.  Otherwise, you may introduce a memory leak.
  • When you allocate memory for an array, you cannot use initializers to initialize values for the array.
  • When you allocate memory for an array, use square brackets, [ ], to release the memory.
  What does it mean, "to move a pointer"?

  Another Example

Simple Example of DMA for char arrays

  Joe's notes on dynamic memory allocation


  A C++ vector is a class template, a container in the C++ Standard Template Library (STL).
  • Can be used as a replacement for an array.
  • Vectors do not have to be dimensioned.
  • Vectors grow automatically, as needed.
  • Memory allocated for a vector does not have to be released.  It is automatically when the vector goes out of scope.
  • Vectors can be indexed, just like an array.
  • To declare a vector, you must specify the type of data to be stored in the vector, using angle brackets.
  • Requires the <vector> header file


  vector<int> vint;          // vint is called a template class

  Some initial useful vector members

constructor (default) creates an empty vector
= assignment operator
size() returns the size of the vector
[ ] index operator (returns rvalue or lvalue)
push_back() adds an element to the end of the vector
pop_back() removes the element at the end of the vector



  Lab Exercise #4

  Put your name, the compiler used, and Lab Exercise #4 in a comment at the top of your program. Email your source code. This lab exercise is due at the beginning of the next lecture

  Write a program that performs the following steps.

  1. Read this file one time to determine how many records it contains.  Be sure to check for a successful file open here.
  2. Close the file.
  3. Allocate memory dynamically to store the data from step 1.  This dynamically allocated memory should be for an array of strings.  The array size should be exactly the number of records from step 1.
  4. Reopen the file and read it a second time, storing each record into the array of strings from step 3.
  5. Print the first two elements and last two elements in the array.
  6. Release the allocated memory.
  7. Your program output should look like this:
Date    Open    High    Low     Close   Volume  Adj Close*
Jan 21, 2016    53.94   53.94   53.94   53.94   0       53.94
Oct 19, 2015    64.10   64.10   64.10   64.10   0       60.03
Oct 16, 2015    64.26   64.26   64.26   64.26   0       60.18