1 es 314 advanced programming lec 3 sept 8 goals: complete discussion of pointers discuss 1-d array...

43
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: • complete discussion of pointers • discuss 1-d array examples • Selection sorting • Insertion sorting • 2-d arrays – images and image representations

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

1

ES 314 Advanced Programming Lec 3

Sept 8

Goals:• complete discussion of pointers

• discuss 1-d array examples• Selection sorting• Insertion sorting

• 2-d arrays – images and image representations

Page 2: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

2

Array Example 1

Write a program that finds the largest number in a given collection of keys.

Assume the numbers are stored in an array.

int max (int[] A, int size)

Page 3: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

3

Array Example

Write a program that finds the largest number in a given collection of keys.

Assume the numbers are stored in an array.

int max (int[] A, int size) { if (size == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; }

Page 4: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

4

Implementation using a vector

int max (vector<int> A, int n) { if (n == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; }

Page 5: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

5

Notion of time complexity

int max (int[] A, int n) { if (n == 0) return 0; int temp = A[0]; for (int j = 1; j < size; ++j) if (temp < A[j]) temp = A[j]; return temp; }

What is the total number of operations performed by the above procedure (as a function of n)?

Assignment, comparison, return – each costs one unit.

Page 6: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Pointers• A pointer is a variable used to store

an address• The declaration of a pointer must

have a data type for the address the pointer will hold (looks like this):

int *ptr; char *chptr;

Page 7: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Location Behavior

• A variable is a location• When locations are used in code, they

behave differently depending on whether or not they are used on the left side of an assignment

• If not used on the left side of an assignment, the value at the location is used

• When used on the left side of assignment, the location itself is used

Page 8: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Address-of operator

• An address can be assigned to a pointer using the address-of operator &:

int *ptr;int x;ptr = &x;

Page 9: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

xptr

Addresses

00110

01010

01110

10010

10110

x

ptr01010

Pointer variable, its value

Page 10: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Dereference Operator

• The dereference operator, *, is used on a pointer (or any expression that yields an address)

• The result of the dereference operation is a location (the location at the address that the pointer stores)

• Therefore, the way the dereference operator behaves depends on where it appears in code (like variables)

Page 11: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

A simple exampleint x = 3, *ptr;

ptrx3

Page 12: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Assignment

ptrx3

ptr = &x;

Page 13: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptrx3

Result of assignment

ptr = &x;

Page 14: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Another assignment

ptrx3

y

y = *ptr + 5;

Page 15: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

15

Another assignment

ptrx3

8 y

y = *ptr + 5;

Page 16: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

More assignmentint z = 5;

ptrx3

y8

z5

Page 17: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

effect of assignment

*ptr = 10 + z; ptr

x

y8

z5

3

Page 18: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptrx

y8

z5

15

*ptr = 10 + z;

Page 19: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

*ptr = 10 + z;

ptrx

y8

z5

15

Note that the value of x changes even though this line of code doesn’t contain x

Page 20: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

20

Exercise: What is the output produced by the following program?

#include <iostream>

int main() {

int * p; int * q;int x; p = &x; *p = 6; cout << x << endl;q = p; *q = 8; cout << *p << endl; q = new int;*q = 9; cout << *q << endl;

return 0;

}

Page 21: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Arrays

• The address of an array is the same as the address of the first element of the array.

• An array’s name (without using an index) contains the address of the array.

• The address of the array can be assigned to a pointer by assigning the array’s name to the pointer.

• An array name is not a pointer because the address in it cannot be changed (the address in a pointer can be changed).

Page 22: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

The [ ] Operator

• When an array is indexed, [ ] is an operator.• For example, nums[3] produces the result

*(nums + 3).• C++ produces an address from the

expression nums + 3, by:– Noting what data type is stored at the address

that nums contains– Multiplying by 3 the number of bytes in that data

type– Adding the product onto the address stored in

nums• After the address is produced from nums +

3, it is dereferenced to get a location.

Page 23: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

The Heap

• The heap is a special part of RAM memory reserved for program usage.

• When a program uses memory from the heap, the used heap memory is called dynamically-allocated memory.

• The only way to dynamically allocate memory (use memory in the heap) is by using the new operator.

Page 24: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

The new Operator• The new operator has only one operand, which is a

data type.• The new operation produces the address of an

unused chunk of heap memory large enough to hold the data type (dynamically allocated)

• In order to be useful, the address must be assigned to a pointer, so that the dynamically allocated memory can be accessed through the pointer:

int *ptr;ptr = new int;*ptr = 5;

Page 25: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

int *ptr;

ptr

Page 26: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr = new int;

ptr

Page 27: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr = new int;

ptr

Page 28: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr = new int;

ptrFound a block in heap

110110

Page 29: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr = 110110;

ptr 110110

Page 30: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr = 110110;

ptr 110110

Page 31: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

*ptr = 5; (what happens?)

ptr 110110

Page 32: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr 110110

5

*ptr = 5;

Page 33: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Dynamically-allocated memory

• Has no name associated with it (other locations have variable names associated with them)

• Can only be accessed by using a pointer• The compiler does not need to know the

size (in bytes) of the allocation at compile time

• The compiler does need to know the size (in bytes) of any declared variables or arrays at compile time

Page 34: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

34

Exercise: What is the output produced by the following program?

#include <iostream>

int main() {

int * p; int * q;int x; p = &x; *p = 6; cout << x << endl;q = p; *q = 8; cout << *p << endl; q = new int;*q = 9; cout << *q << endl;

return 0;

}

Page 35: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Memory Leak

First, pointer ptr is assigned the address of dynamically allocated memory.

ptr110110

5

Page 36: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Pointer assigned again

ptr110110

5

ptr = new int; (what happens?)

Page 37: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr = new int;

ptr110110

5

Unused block found in heap

111000

Page 38: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Pointer assignment changes

ptr110110

5

111000

Page 39: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr110110

5

111000

Pointer assignment

Page 40: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

ptr110110

5

111000

The address of this block is not stored anywhere, but it hasn’t been freed – memory leak

Memory location inaccessible

Page 41: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Avoiding Memory Leak

• When you want to change the address stored in a pointer, always think about whether or not the current address is used for dynamically-allocated memory

• If it is (and that memory is no longer useful), use the delete operator before changing the address in the pointer; for example,

delete ptr;

Page 42: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Pointers to Objects

• Pointers can be used to point to objects or arrays of objects:

Check *chkptr = new Check;Check *chkarrptr = new Check [10];

• The delete operator is used the same way:

delete chkptr;delete [ ] chkarrptr;

Page 43: 1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays

Running out of Heap Memory

• We can run out of heap memory if:– We write poor code that continually allows

memory leak while constantly using the new operator

– OR …– If we use excessive amounts of heap memory

• If the new operator cannot find a chunk of unused heap memory large enough for the data type, the new operation throws an exception