exercise 7 pointers - cgl @ ethz - homechschuma/info1_13/exercise7...memory location this is where...

26
Informatik I für D-MAVT (FS 2013) Exercise 7 Pointers Christian Schumacher [email protected]

Upload: hoangtruc

Post on 26-Mar-2018

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

Informatik I für D-MAVT (FS 2013)

Exercise 7 – Pointers

Christian Schumacher

[email protected]

Page 2: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Agenda

Pointers

Defining pointers

Reference operator &

Dereference operator *

Dynamic memory allocation

new and delete

Pointers and arrays

Pointer arithmetic

Dynamic array allocation

2

Page 3: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Overview

Computer memory

Large collection of consecutive memory blocks (1 Byte == 8 bit)

Each memory block has a unique address

Whenever a variable is defined, it is assigned to a

memory location

This is where its value is stored

A pointer is a variable that points to another variable

It stores the memory address of the variable it points to

instead of the value itself

3

Page 4: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Syntax

Definition

Example:

Attention while defining multiple pointers

* has to be in front of every pointer

4

TypeName *VariableName;

int *pointer;

int *a, *b, *c;

Page 5: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Syntax

Reference operator &

Returns the address of a variable

Dereference operator *

Accesses the memory location of a pointer

5

float a = 1.5; // float variable

float *p; // pointer to float

p = &a; // p points to a

// (p stores address of a)

float b = *p; // assign value at location p to b

*p = 3.2; // assign 3.2 to memory location p

Page 6: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Syntax

Don’t confuse * with *

6

int a, b;

int *p;

p = &a;

*p = 5;

b = *p;

pointer declaration

dereference operator

Page 7: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Example

7

Memory

address

0x1000

0x1004

0x1008

0x100C

0x1010

int a, b;

a

b

Page 8: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Example

8

Memory

address

0x1000

0x1004

0x1008

0x100C

0x1010

int a, b;

a = 12; b = 5;

a

b

12

5

Page 9: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Example

9

Memory

address

0x1000

0x1004

0x1008

0x100C

0x1010

int a, b;

a = 12; b = 5;

int *p;

a

b

12

5

p

Page 10: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Example

10

Memory

address

0x1000

0x1004

0x1008

0x100C

0x1010

int a, b;

a = 12; b = 5;

int *p;

p = &a;

a

b

12

5

p 0x1000

Page 11: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Example

11

Memory

address

0x1000

0x1004

0x1008

0x100C

0x1010

int a, b;

a = 12; b = 5;

int *p;

p = &a;

*p = 36;

a

b

36

5

p 0x1000

Page 12: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Example

12

Memory

address

0x1000

0x1004

0x1008

0x100C

0x1010

int a, b;

a = 12; b = 5;

int *p;

p = &a;

*p = 36;

b = *p;

a

b

36

36

p 0x1000

Page 13: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Example

13

Memory

address

0x1000

0x1004

0x1008

0x100C

0x1010

int a, b;

a = 12; b = 5;

int *p;

p = &a;

*p = 36;

b = *p;

a = 23;

a

b

23

36

p 0x1000

Page 14: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers: Example

14

Memory

address

0x1000

0x1004

0x1008

0x100C

0x1010

int a, b;

a = 12; b = 5;

int *p;

p = &a;

*p = 36;

b = *p;

a = 23;

cout << *p; // prints: 23

cout << p; // prints: 0x1000

a

b

23

36

p 0x1000

Page 15: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers

15

Page 16: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers

Pointers are important for

Allocating memory during runtime (instead of compile

time)

e.g., if you want to declare an array, but decide on the size

of the array while the program is running more flexibility

Passing pointers as function arguments

This allows the function to change values outside the scope

of the function

The exam!

16

Page 17: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Dynamic Memory Allocation

new operator

Allocates memory and returns address

Memory needs to be freed manually

delete operator

Frees memory at given address

For arrays, use delete[] instead of delete

17

int *a_p = new int;

*a_p = 5;

delete a_p;

Page 18: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Static

Syntax:

Memory valid only as long as

variable is valid (scope)

Memory is freed implicitly at

the end of the scope

Dynamic

Syntax:

Memory location valid until delete is called

Memory is blocked until freed

explicitly

18

int x; int *x = new int;

Dynamic Memory Allocation

Page 19: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointers and arrays

Arrays are internally represented by pointers

myArray and &myArray[0] are the same, they

are the address of the first element of the array

19

int myArray[5] = {1,2,3,4,5}; // static array

int *pArray; // pointer

pArray = &myArray[0]; // pointer to 1st element

pArray = myArray; // pointer to 1st element

Page 20: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointer Arithmetic

Increment ++ / Decrement –-

Move pointer one element forward or backward

Add + / Subtract -

Returns new pointer moved by an arbitrary number of

elements

Example:

20

int a[5] = {0};

int *a_p = a;

*(a_p + 4) = 4;

*++a_p = 1;

*a_p++ = 2;

0 0 0 0 0

0 0 0 0 4

0 1 0 0 4

0 2 0 0 4

Page 21: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Pointer Arithmetic

[] brackets vs. dereferencing operator *

21

int tacos[5];

tacos[0] *tacos the value at address tacos

tacos[3] *(tacos+3) value at address (tacos + 3)

Page 22: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Dynamic Array Allocation

With new, the array size can be specified at run time

Example:

For arrays, use delete[] instead of delete

22

int size;

cin >> size;

// Allocate memory of the needed size

int *a_p = new int[size];

a_p[3] = 5; // Use as regular array

// Free memory

delete[] a_p;

Page 23: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Dynamic Struct Allocation

Another example with structs:

23

struct Student // Structure delcaration

{

char vorname[20];

char name[20];

int legi;

};

Student* stud1 = new Student; // Dynamic allocation

(*stud1).legi = 23432; // Assign values

strcpy(stud1->vorname,"Hans");

strcpy(stud1->name,"Muster");

// print

cout << stud1->vorname << " " << stud1->name << endl;

cout << stud1->legi << endl;

delete stud1; // Free memory

Page 24: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Summary

Pointers store the address to a value, not the value

itself

To access the value, use the dereference operator *

To get the address of a variable, use the reference operator &

Use new to create arrays of arbitrary size

Always use delete or delete[] if you use new

24

Page 25: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Übung 7

Aufgabe 3: Vektoren

a) Vektor kann als Array dargestellt werden

• Array mit dynamischer Grösse muss mit new erstellt werden

b) for-Schleife verwenden

c) float computeLength(float *vecPtr, int dim)

• Array == Pointer auf erstes Element vecPtr kann wie ein

normales Array verwendet werden

• Wurzel: float a = 1.0f;

float b = sqrt(a);

25

Page 26: Exercise 7 Pointers - CGL @ ETHZ - Homechschuma/info1_13/exercise7...memory location This is where its value is stored A pointer is a variable that points to another variable It stores

© M. Gross, ETH Zürich, 2013

Übung 7

Aufgabe 4: Punkteliste

b) rand() gibt eine zufällige Ganzzahl zwischen 0 und

RAND_MAX zurück

• Modulo-Operator (%) zum Verkleinern des Bereichs

• Beispiel: int a = rand() % 50; liefert eine Ganzzahl

zwischen 0 und 49

26