Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 1
Lecture 15
No in-class files today. Reminder: Project 3 due on Friday. Homework 4 posted, due next Monday. Questions?
Lecture 15 No in-class files today. Reminder: Project 3 due on - - PowerPoint PPT Presentation
Lecture 15 No in-class files today. Reminder: Project 3 due on Friday. Homework 4 posted, due next Monday. Questions? Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 1 Outline Static memory allocation
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 1
No in-class files today. Reminder: Project 3 due on Friday. Homework 4 posted, due next Monday. Questions?
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 2
Static memory allocation Pointer variables Pointers Dynamic memory allocation
new and delete garbage and dangling references
Dynamic arrays
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 3
If we want to implement a class like vector<T>,
Consider the following declarations
The sizes of these variables depends on the
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 4
Exactly how memory is addressed also
Statically allocated memory is allocated at
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 5
'x' 'y'
0x00001220 0x00001221 0x00001222 0x00001223 0x00001224 0x00001228 0x00001229 0x0000122a 0x00001232
3 4 2 . 5 3 . 6
i j a b d e
address variable name
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 6
Pointer variables are regular variables and also
Consider the following additional declarations:
Pointers are usually the same size as an int;
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 7
0x0000122a 0x00001232
2 . 5 3 . 6
d e 0x0000123a 0x0000123e 0x00001242 iPtr1 iPtr2 cPtr1 cPtr2 0x00001246 0x0000124a 0x0000124e dPtr1 dPtr2
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 8
What is a pointer? An address. How do we get an address? One way is to use
As in C, the type of the pointer must match the
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 9
0x0000122a 0x00001232
2.5 3.6
d e 0x0000123a 0x0000123e 0x00001242 iPtr1 iPtr2 cPtr1 cPtr2 0x00001246 0x0000124a 0x0000124e dPtr1 dPtr2 0x1220 0x1224 0x1228 0x1229 0x122a 0x1232
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 10
Generally, we do not care about the actual
iPtr1 i pointer variable pointer "pointee" (i.e., the thing being pointed to)
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 11
How do we access the "pointee"? Use the
Note: need to be careful with char pointers,
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 12
What happens when the following code is
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 13
3 iPtr1 i 4 iPtr2 j 4 (a) XX (b)
Can test two pointer variables for equality (==,
E.g. before statement (b) is executed,
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 14
iPtr1 iPtr2
Special value to represent not pointing to
Drawn in a few different ways:
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 15
Pointing to an already existing variable is not
Dynamically allocated memory comes from a
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 16
In C++, dynamic memory allocation is done
This allocates an anonymous variable for one
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 17
The anonymous variable is just like a statically
iPtr1 iPtr2
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 18
After the program is done using an anonymous
This is done using the delete operator, which
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 19
Note delete deallocates the anonymous
iPtr1 iPtr2
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 20
iPtr1 iPtr2 XX garbage dangling references
Note that if we execute iPtr1 = iPtr2; before
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 21
Dynamically allocating single variables is not
The size can be any positive integer expression
[0] [1] [2] [3] [4] intArrPtr
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 22
Back up a bit. Here is a statically allocated
As noted before, the name of an array is the
[0] [1] [2] [3] [4] array
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 23
We can dereference these addresses to access
This also is true for dynamic arrays using the
Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 24
Thus the syntax for accessing a dynamic array
Can even say intArrPtr = array; Must tell the compiler when deallocating an
Note: the [ ] is empty, and delete must match