COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


Pointers




(page 2)


Pointers




(page 3)


Declaring Pointers




(page 4)


Declaring Pointers




(page 5)


Getting an Address




(page 6)


Pointer Example


int main() {
int i = 1;
double d = 2.3;
std::string s = "4";
std::cout << "&i : " << &i << endl;
std::cout << "&d : " << &d << endl;
std::cout << "&s : " << &s << endl;
}


(page 7)


Dereferencing





(page 8)


Dereferencing Example


int main() {
int i = 42;
int *p = &i;
std::cout << i << ' ' << *p << endl;
(*p)++;
std::cout << i << ' ' << *p << endl;
}
42 42
43 43


(page 9)


Null pointers




(page 10)


Null pointer example


void output(int* p) {
std::cout << *p << std::endl;
}
int main() {
int i = 42;
int *p = &i;
int *q = NULL;
output(p);
output(q); // CRASH
}


(page 11)


Better null pointer example


void output(int* p) {
if (p)
std::cout << *p << std::endl;
}
int main() {
int i = 42;
int *p = &i;
int *q = NULL;
output(p);
output(q);
}


(page 12)


Function Pointers




(page 13)


Function Pointer Example


void swap(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}


(page 14)


int main() {
int x = 1, y = 3;
void (*funcpoint)(int&, int&);
funcpoint = &swap;
swap(a, b);
std::cout << a << ' ' << b << endl;
(*funcpoint)(a, b);
std::cout << a << ' ' << b << endl;
}
3 1
1 3


(page 15)


Functions




(page 16)


Function Pointer Example


int main() {
int x = 1, y = 3;
void (*funcpoint)(int&, int&);
funcpoint = swap;
swap(a, b);
std::cout << a << ' ' << b << endl;
funcpoint(a, b);
std::cout << a << ' ' << b << endl;
}


(page 17)


Why Function Pointers




(page 18)


Simple example: find_first


int find_first(const vector &v,
bool pred(int)) {
for (int i = 0; i < v.size(); i++) {
if (pred(v[i])) {
return i;
}
}
return -1;
}


(page 19)


bool even(int i) {
return (i % 2 == 0);
}
bool positive(int i) {
return (i > 0);
}

vector v;
std::cout << find_first(v, even) << endl;
std::cout << find_first(v, positive) << endl;


(page 20)


Arrays


int an_array[4];


(page 21)


Array Initialisation




(page 22)


Array Initialisation




(page 23)


Array Example


const size_t P_Dim = 3;
double point[P_Dim];
*point = 42.0;


(page 24)


Pointer Arithmetic




(page 25)


Pointer Arithmetic Example


const size_t P_Dim = 3;
double array[P_Dim];
double* point;
*array = 42.0;
*(array + 1) = 42.5;
*(array + 2) = 43.0;
point = array + 1;
cout << *(point + 1) << endl;


(page 26)


Indexing




(page 27)


Indexing Example


const size_t P_Dim = 3;
double array[P_Dim];
double* point;
point[0] = 42.0;
point[1] = 42.5;
point[2] = 43.0;
point = array + 1;
cout << point[1] << endl;


(page 28)


Array-based find_first


int find_first(const int *v, int n,
bool pred(int)) {
for (int i = 0; i < n; i++) {
if (pred(v[i])) {
return i;
}
}
return -1;
}


(page 29)


int v[ ] = {-1, -3, -5, 3, -2};
cout << find_first(v, 5, even) << endl;
cout << find_first(v, 5, positive) << endl;
cout << find_first(v, 3, even) << endl;
cout << find_first(v, 3, positive) << endl;


(page 30)


Iterator-based find_first


int* find_first(const int* begin,
const int* end,
bool pred(int)) {
while (begin != end && !pred(*begin))
begin++;
return begin;
}


(page 31)


int v[ ] = {-1, -3, -5, 3, -2};
int* p = find_first(v, v + 5, even);
if (p == v + 5) {
cout << "No even nums found";
} else {
cout << "First even num: " << *p;
}
cout << endl;



(page 32)


String literals




(page 33)


C-Style Strings




(page 34)


Arguments to main()




(page 35)


main() Example


int main(int argc, char** argv) {
std::cout << "Program name: "
<< argv[0] << std::endl;
for (int i = 0; i < argc; ++i)
std::cout << "Arg " << i <<
" is: " << argv[i] << std::endl;
}


(page 36)


Arguments Example


bash$ g++ -Wall -g -o argex argex.cc bash$ argex These are the arguments Program name: argex
Arg 0 is: argex
Arg 1 is: These
Arg 2 is: are
Arg 3 is: the
Arg 4 is: arguments


(page 37)


Memory Allocation




(page 38)


Automatic Variables




(page 39)


Automatic Example


void do_something(int *p) {
}

int* func() {
int i = 42;
do_something(&i); // OK
return &i; // NOT OK!
}


(page 40)


Static Allocation




(page 41)


Static Example


int* func() {
static int i = 42;
i++;
return &i;
}
int main() {
int* p1 = func();
int* p2 = func();
cout << p1 << " : " << *p1 << endl;
cout << p2 << " : " << *p2 << endl;
}


(page 42)


Dynamic Allocation


(page 43)


New





(page 44)


Delete




(page 45)


Dynamic Example


int* func() {
return new int(42);
}
int main() {
int* p1 = func();
int* p2 = func();
cout << p1 << " : " << *p1 << endl;
cout << p2 << " : " << *p2 << endl;
delete p1;
delete p2;
}


(page 46)


Dynamic Allocation of Arrays




(page 47)


Dynamic Array Example


int* read_data(size_t n) {
int* a = new int[n];
for (size_t i = 0; i < n; ++i)
std::cin >> a[i];
return a;
}


(page 48)


Dynamic Array Example


int main() {
int* data = read_data(5);
for (size_t i = 0; i < 5; ++i)
std::cout << data[i] << std::endl;
delete[ ] data;
}


(page 49)


What about this?


int main() {
int* data;
data = read_data(5);
for (size_t i = 0; i < 5; ++i)
std::cout << data[i] << std::endl;
data = read_data(5);
for (size_t i = 0; i < 5; ++i)
std::cout << data[i] << std::endl;
delete[ ] data;
}


(page 50)


Memory leaks




(page 51)


Leak-free code


int main() {
int* data;
data = read_data(5);
for (size_t i = 0; i < 5; ++i)
std::cout << data[i] << std::endl;
delete[ ] data;
data = read_data(5);
for (size_t i = 0; i < 5; ++i)
std::cout << data[i] << std::endl;
delete[ ] data;
}


(page 52)


Avoiding memory leaks





(page 53)