Download presentation
Presentation is loading. Please wait.
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 – images and image representations
2
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)
3
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; }
4
4 Implementation using a vector int max (vector 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; }
5
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.
6
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;
7
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
8
Address-of operator An address can be assigned to a pointer using the address-of operator &: int *ptr; int x; ptr = &x;
9
x ptr Addresses 00110 01010 01110 10010 10110 x ptr 01010 Pointer variable, its value
10
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)
11
A simple example int x = 3, *ptr; ptr x3
12
Assignment ptr x3 ptr = &x;
13
ptr x3 Result of assignment ptr = &x;
14
Another assignment ptr x3 y y = *ptr + 5;
15
15 Another assignment ptr x3 8 y y = *ptr + 5;
16
More assignment int z = 5; ptr x3 y8 z5
17
effect of assignment *ptr = 10 + z; ptr x y8 z5 3
18
x y8 z5 15 *ptr = 10 + z;
19
ptr x y8 z5 15 Note that the value of x changes even though this line of code doesn’t contain x
20
20 Exercise: What is the output produced by the following program? #include 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; }
21
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).
22
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.
23
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.
24
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;
25
int *ptr; ptr
26
ptr = new int; ptr
27
ptr = new int; ptr
28
ptr = new int; ptr Found a block in heap 110110
29
ptr = 110110; ptr 110110
30
ptr = 110110; ptr 110110
31
*ptr = 5; (what happens?) ptr 110110
32
ptr 110110 5 *ptr = 5;
33
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
34
34 Exercise: What is the output produced by the following program? #include 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; }
35
Memory Leak First, pointer ptr is assigned the address of dynamically allocated memory. ptr 110110 5
36
Pointer assigned again ptr 110110 5 ptr = new int; (what happens?)
37
ptr = new int; ptr 110110 5 Unused block found in heap 111000
38
Pointer assignment changes ptr 110110 5 111000
39
ptr 110110 5 111000 Pointer assignment
40
ptr 110110 5 111000 The address of this block is not stored anywhere, but it hasn’t been freed – memory leak Memory location inaccessible
41
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;
42
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;
43
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.