Lecture 15 No in-class files today. Reminder: Project 3 due on - - PowerPoint PPT Presentation

lecture 15
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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?

slide-2
SLIDE 2

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 2

Outline

 Static memory allocation  Pointer variables  Pointers  Dynamic memory allocation

 new and delete  garbage and dangling references

 Dynamic arrays

slide-3
SLIDE 3

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 3

Static Memory Allocation

 If we want to implement a class like vector<T>,

we need dynamically allocated memory;

  • therwise the data structure could not grow

indefinitely

 Consider the following declarations

int i = 3, j = 4; char a = 'x', b = 'y'; double d = 2.5, e = 3.6;

 The sizes of these variables depends on the

architecture, but often 4, 1, and 8 or 10 bytes.

slide-4
SLIDE 4

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 4

Static Memory Allocation

 Exactly how memory is addressed also

depends on the architecture. E.g., byte, 16-bit word, 32-bit word. Will assume byte- addressable, 32-bit words. Addresses will be written in hexadecimal (base 16) => 8 digits.

 Statically allocated memory is allocated at

compile-time before the program runs. It is allocated from a stack, which is usually at one end of memory.

slide-5
SLIDE 5

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 5

Static Memory Allocation

'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

slide-6
SLIDE 6

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 6

Pointer Variables

 Pointer variables are regular variables and also

are allocated at compile-time.

 Consider the following additional declarations:

// note the repeated *'s int *iPtr1, *ptr2; char *cPtr1, *cPtr2; double *dPtr1, *dPtr2;

 Pointers are usually the same size as an int;

4 bytes in our example.

slide-7
SLIDE 7

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 7

Pointer Variables

0x0000122a 0x00001232

2 . 5 3 . 6

d e 0x0000123a 0x0000123e 0x00001242 iPtr1 iPtr2 cPtr1 cPtr2 0x00001246 0x0000124a 0x0000124e dPtr1 dPtr2

slide-8
SLIDE 8

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 8

Pointers

 What is a pointer? An address.  How do we get an address? One way is to use

the & operator to get the address of a variable. E.g.,

iPtr1 = &i; // iPtr1 points to i iPtr2 = &j; cPtr1 = &a; cPtr2 = &b;

 As in C, the type of the pointer must match the

type of the pointer variable.

slide-9
SLIDE 9

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 9

Pointers

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

slide-10
SLIDE 10

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 10

Pointers

 Generally, we do not care about the actual

  • address. Represent a pointer as an arrow

starting in a pointer variable and ending at the "pointee" (i.e., the thing being pointed to)

iPtr1 i pointer variable pointer "pointee" (i.e., the thing being pointed to)

slide-11
SLIDE 11

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 11

Using Pointers

 How do we access the "pointee"? Use the

unary * operator to dereference the pointer

  • variable. E.g.

double f = *dPtr2; // set f to 3.6 cout << "iPtr1 points to " << *iPtr1 << endl; // displays 3

 Note: need to be careful with char pointers,

since C/C++ interprets them as null-terminated

  • strings. Won't use them much.
slide-12
SLIDE 12

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 12

Using Pointers

 What happens when the following code is

executed?

// (a) pointee assignment *iPtr1 = *iPtr2; // (b) pointer variable assignment iPtr1 = iPtr2;

slide-13
SLIDE 13

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 13

Using Pointers

3 iPtr1 i 4 iPtr2 j 4 (a) XX (b)

 Can test two pointer variables for equality (==,

point to same location) or inequality (!=).

 E.g. before statement (b) is executed,

iPtr1 == iPtr2 is false, but afterwards in the situation shown above, iPtr == iPtr2 is true.

slide-14
SLIDE 14

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 14

Using Pointers

iPtr1 iPtr2

 Special value to represent not pointing to

  • anything. Called null pointer and is written as

0 (not NULL). E.g. iPtr1 = 0;

 Drawn in a few different ways:

slide-15
SLIDE 15

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 15

Dynamic Memory Allocation

 Pointing to an already existing variable is not

very interesting. Power of pointers comes with dynamically allocated memory. I.e., memory allocated during program execution (aka run- time).

 Dynamically allocated memory comes from a

heap, usually located at the other end of memory from the stack.

slide-16
SLIDE 16

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 16

Dynamic Memory Allocation

 In C++, dynamic memory allocation is done

using the new operator. It has the following syntax

new <type>;

 This allocates an anonymous variable for one

item of the given type and returns the address

  • f (i.e., pointer to) that piece of memory. E.g.,

iPtr1 = new int; iPtr2 = new int;

slide-17
SLIDE 17

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 17

Dynamic Memory Allocation

 The anonymous variable is just like a statically

allocated one, except that it has no name, so the only way to access the "pointee" is through dereferencing a pointer variable that points to it.

iPtr1 iPtr2

slide-18
SLIDE 18

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 18

Dynamic Memory Deallocation

 After the program is done using an anonymous

variable, it needs to deallocate the variable. I.e., give it back to the system.

 This is done using the delete operator, which

has syntax: delete <pointer var>; E.g.

delete iPtr1; // dealloc. the pointee delete iPtr2;

slide-19
SLIDE 19

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 19

Dynamic Memory Deallocation

 Note delete deallocates the anonymous

pointee, not the named pointer variable, as shown above.

iPtr1 iPtr2

slide-20
SLIDE 20

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 20

Garbage & Dangling References

iPtr1 iPtr2 XX garbage dangling references

 Note that if we execute iPtr1 = iPtr2; before

doing the deletes, we lose the pointer to the first allocated space and cannot deallocate it. This is called a memory leak or garbage. And the fact that both iPtr1 and iPtr2 point to deallocated space is called dangling references.

slide-21
SLIDE 21

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 21

Dynamic Arrays

 Dynamically allocating single variables is not

very interesting either. The real power is in dynamically allocating arrays. C++ syntax is:

<type> *ptr = new <type>[<size>];

 The size can be any positive integer expression

including a variable. E.g.

cin >> n; // assume user enters 5 int *intArrPtr = new int[n];

[0] [1] [2] [3] [4] intArrPtr

slide-22
SLIDE 22

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 22

Array Addressing

 Back up a bit. Here is a statically allocated

array:

int array[5];

 As noted before, the name of an array is the

address of the first element of the array. Also, pointer arithmetic is scaled:

(array+0) // address of array[0] (array+1) // address of array[1] (array+2) // address of array[2] // etc.

[0] [1] [2] [3] [4] array

slide-23
SLIDE 23

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 23

Array Addressing

 We can dereference these addresses to access

the elements, meaning that:

*(array+0) == array[0] *(array+1) == array[1] // etc.

 This also is true for dynamic arrays using the

pointer variable to provide the pointer to the first element of the dynamic array.

*(intArrPtr+0) == intArrPtr[0] *(intArrPtr+1) == intArrPtr[1] // etc.

slide-24
SLIDE 24

Monday, September 27 CS 215 Fundamentals of Programming II - Lecture 15 24

Dynamic Arrays

 Thus the syntax for accessing a dynamic array

is exactly the same as for a static array. E.g.,

for (int i = 0; i < n; i++) intArrPtr[i] = 0;

 Can even say intArrPtr = array;  Must tell the compiler when deallocating an

array by giving [ ] after delete. E.g.,

delete [] intArrPtr;

 Note: the [ ] is empty, and delete must match

up with corresponding new