You are on page 1of 29

Pointers

December 11, 2017

0 of 20
Pointers

Definition
A pointer is a data type that “points” to another value stored
in a memory.

Syntax
int a = 5;
int* p = &a;

1 of 20
Review Exercises

Exercise P1
Trace the following code. Assume that a and b are stored at
20300 and 20308. Your trace table should have entries for
a, b, and p.

double a = 1000;
double b = 2000;
double* p = &a;
*p = 3000;
p = &b;
a = *p * 2;

2 of 20
Review Exercises

Exercise P2
Trace the following code. Assume that a and b are stored at
20300 and 20308. Your trace table should have entries for
a, b, p, and q.

double a = 1000;
double b = 2000;
double* p = &a;
double* q = &b;
*p = *q;
p = q;
*p = 3000;

3 of 20
Review Exercises

Exercise P3
What does the following code prints.

double a = 1000;
double b = 2000;
double* p = &a;
double* q = p;
b = *q;
p = &b;
a = *p + *q;
cout << a << " " << b << endl;

4 of 20
Review Exercises

Exercise P4
Explain the mistakes in the following code. Not all lines
contain mistakes. Each line depends on the lines preceding it.

1. double a = 1000;
2. double* p = a;
3. int* p = &a;
4. double* q;
5. *q = 2000;
6. int* r = NULL;
7. *r = 3000;

5 of 20
Arrays and Pointers

Definition
-

Syntax
int a = 5;
int* p = &a;

6 of 20
Review Exercises

Exercise P5
Suppose the array a starts at memory location 20300.
double a[] = {2, 3, 5, 7, 11, 13};
What are the values of
1. a
2. *a
3. a+4
4. *(a + 4)
5. a[4]
6. &a[4]

7 of 20
Arrays and Pointers

Array Function
double sum (double a[], int size)
{
double total = 0;
for (int i = 0; i < size; i++)
{
total = total + a[i];
}
return total;
}

8 of 20
Arrays and Pointers

Array Function - Using a Pointer


double sum (double *a, int size)
{
double total = 0;
double* p = a;
for (int i = 0; i < size; i++)
{
total = total + *p;
p++;
}
return total;
}

9 of 20
Dynamic Memory Allocation

Definition
-

Syntax
double∗ accountpointer = new double;
double∗ accountarray = new double[10];

10 of 20
Dynamic Memory Allocation

Things to Remember
Every call to new must be matched by exactly one call to
delete.
Use delete[] to delete arrays.
Don’t access memory block after it has been deleted.
You can only delete memory blocks that you obtained
from calling new.

11 of 20
Dynamic Memory Allocation - Common Errors

Statement 01
int∗ p;
∗p = 5;
delete p;

12 of 20
Dynamic Memory Allocation - Common Errors

Statement 01
int∗ p;
∗p = 5;
delete p;

Error
There is no call to new int

12 of 20
Common Memory Allocation Errors

Statement 02
int∗ p = new int;
∗p = 5;
p = new int;

13 of 20
Common Memory Allocation Errors

Statement 02
int∗ p = new int;
∗p = 5;
p = new int;

Error
The first allocated memory block was never deleted.

13 of 20
Common Memory Allocation Errors

Statement 03
int∗ p = new int[10];
∗p = 5;
delete p;

14 of 20
Common Memory Allocation Errors

Statement 03
int∗ p = new int[10];
∗p = 5;
delete p;

Error
The delete[] operator should have been used.

14 of 20
Common Memory Allocation Errors

Statement 04
int∗ p = new int[10];
int∗ q = p;
q[0] = 5;
delete p;
delete q;

15 of 20
Common Memory Allocation Errors

Statement 04
int∗ p = new int[10];
int∗ q = p;
q[0] = 5;
delete p;
delete q;

Error
The same memory block was deleted twice.

15 of 20
Common Memory Allocation Errors

Statement 05
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

16 of 20
Common Memory Allocation Errors

Statement 05
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

Error
You can only delete memory blocks that you obtained from
calling new.

16 of 20
Common Memory Allocation Errors

Dangling Pointers
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

Memory Leaks
A memory block that is never deallocated is called a memory
leak.

17 of 20
Review Exercises

Exercise 1
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

18 of 20
Review Exercises

Exercise 1
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

Error
You can only delete memory blocks that you obtained from
calling new.

18 of 20
Structures

Statement 05
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

19 of 20
Structures

Statement 05
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

Error
You can only delete memory blocks that you obtained from
calling new.

19 of 20
Review Exercises

Exercise 1
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

20 of 20
Review Exercises

Exercise 1
int n = 4;
int∗ p = &n;
∗p = 5;
delete p;

Error
You can only delete memory blocks that you obtained from
calling new.

20 of 20

You might also like