CIS 22B - Notes for Mon Oct 16

  Announcements and Reminders

  • Assignment 3 is due NOW
  • Teacher's Aide, Joshua Kuan, is available for your help in the lab on Thursday & Friday 10:30-11:30
  • Presentation tomorrow, 4:30-6:00 pm, ATC Lab:  Mike Gainer, Senior Developer from Google - Design at scale: How to do things when the task absolutely cannot fit on one machine, or even a few machines

  Comments on Assignment 3

  • Stay away from sstream and the use of stringstream classes
  • Use the sample output (watch out for rounding)
  • Follow the stated requirements

  Pointers Continued

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

  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
Examples:

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

or

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];
strcpy(pName,"Harry");
...
cout << *pName;
...
delete [] pName;

  Notes:
  • 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


  Vectors

  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

  Example

  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

Reference

Example

  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 and last record in the array.
  6. Release the allocated memory.
  7. Your program output should look like this:
Date    Open    High    Low     Close   Volume  Adj Close*
Oct 16, 2015    64.26   64.26   64.26   64.26   0       60.18