- With one-dimensional arrays
- With two-dimensional arrays
pointer to pointer
pointer to const, const pointer
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
new and delete operators
- new allocates memory
- delete releases memory
ptr = new int;
*ptr = 6;
int* ptr = new
Where is ptr stored? Where is the dynamic memory?
Release the memory
float* ptrPI =
char* pc = new
*pc = 'x';
dog* pSirius =
Allocate memory for an array
int * pA = new
for (int i =
0; i < 5; i++)
pA[i] = 5 * i;
delete  pA;
char* pName =
What does it mean, "to move a pointer"?
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.
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
// vint is called a template class
Some initial useful vector members
||creates an empty vector
||returns the size of the vector
||index operator (returns rvalue or lvalue)
||adds an element to the end of the vector
||removes the element at the end of the vector
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.
a program that performs the following steps.
- Read this file
one time to determine how many records it contains. Be sure
to check for a successful file open here.
- Close the file.
- 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.
- Reopen the file and read it a second time, storing
each record into the array of strings from step 3.
- Print the first two elements and last two elements in the array.
- Release the allocated memory.
- Your program output should look like this:
Low Close Volume Adj Close*
21, 2016 53.94 53.94
2015 64.10 64.10
2015 64.26 64.26