data structures and algorithms lab5
DESCRIPTION
TRANSCRIPT
![Page 1: Data structures and algorithms lab5](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/1.jpg)
DATA STRUCTURES AND ALGORITHMS
LAB 5
Bianca Tesila
FILS, March 2014
![Page 2: Data structures and algorithms lab5](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/2.jpg)
OBJECTIVES
Pointers
![Page 3: Data structures and algorithms lab5](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/3.jpg)
POINTERS: INTRODUCTION
What is a pointer?
![Page 4: Data structures and algorithms lab5](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/4.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/5.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/6.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/7.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/8.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/9.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022100318/545c56edb0af9fae2c8b47c0/html5/thumbnails/10.jpg)
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.