data structures and algorithms lab5

10

Click here to load reader

Upload: bianca-tesila

Post on 07-Nov-2014

1.126 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Data structures and algorithms lab5

DATA STRUCTURES AND ALGORITHMS

LAB 5

Bianca Tesila

FILS, March 2014

Page 2: Data structures and algorithms lab5

OBJECTIVES

Pointers

Page 3: Data structures and algorithms lab5

POINTERS: INTRODUCTION

What is a pointer?

Page 4: Data structures and algorithms lab5

POINTERS: SHORT REMINDER FROM THE LECTURES

Memory space of a program=a sequence of bytes The bytes are numbered starting from 0: 0, 1, 2 …

Every variable occupies a certain number of consecutive bytes in memory An int occupies 4 bytes A double occupies 8 bytes A char occupies 1 byte

The address of a variable = the index of the first byte occupied in memory If an int variable occupies the bytes numbered 1000,

1001, 1002, 1003, the address of the variable is 1000

Page 5: Data structures and algorithms lab5

POINTERS: SHORT REMINDER FROM THE LECTURES

Every variable stores its value in the bytes it occupies in the program’s memory A char stores its value in 1 byte An int stores its value as 4 consecutive bytes

Pointer = special type of variable Its size = architecture-dependent (32 bits = 4 bytes in our

case) Stores an address (the index of a byte in memory) The pointer points to the address it stores Multiple types of pointers

int* p (pointer to int) char* p (pointer to char) int** p (pointer to pointer to int) double*** p ... Each type of pointer has 4 bytes and stores an address Differences:

int x; int *p, char* q; p = q = &x; *p // refers to the int which starts at the address &x *q // refers to the char located at the address &x (the first byte of the 4-

byte int variable x)

Page 6: Data structures and algorithms lab5

POINTERS: SHORT REMINDER FROM THE LECTURES

!!Example:

int x;int *p;char* q;

p = q = &x;

*p // refers to the int which starts at the address &x

*q // refers to the char located at the address &x (the first byte of the 4-byte int variable x)

Page 7: Data structures and algorithms lab5

POINTERS: OPERATORS

& (reference operator)

The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of".

myvar = 25; foo = &myvar; bar = myvar;

The variable that stores the address of another variable (like foo in the previous example) is what in C++ is called a pointer.

Page 8: Data structures and algorithms lab5

POINTERS: OPERATORS

* (deference operator)

An interesting property of pointers is that they can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*). The operator itself can be read as "value pointed to by".

baz = *foo;

This could be read as: "baz equal to value pointed to by foo", and the statement would actually assign the value 25 to baz, since foo is 1776, and the value pointed to by 1776 (following the example above) would be 25.

Page 9: Data structures and algorithms lab5

POINTERS

C++ has both pointers and references, whereas Java has only references. C++ pointers are manipulated more like Java references. You cannot manipulate (using assignment statements) C++ references at all.

The main difference between C++ pointers and references (in Java or C++) is that the compiler manages references (knows you want the contents, not the address) whereas you (the programmer) must de-reference pointers to get at data. In other words, the compiler automatically does the de-referencing when it's a reference.

To allocate memory, as in Java, use the keyword new . To deallocate memory, use delete. There is no garbage collection so all memory allocated by programmer must be deallocated by the programmer. The compiler manages memory that it allocates, e.g., int n;   The compiler allocates memory for   n  and deallocates its memory when it goes out of scope.

Page 10: Data structures and algorithms lab5

POINTERS

For the beginning, let’s analyze PointersExample.cpp

!!Exercise:

Implement a function that reverses the elements of an array.

Hint: void invert(int *array, int arraySize)

An array variable contains the starting address of the array (the array element of the first address) and a pointer that is equal to the type of array elements.