Download presentation
Presentation is loading. Please wait.
Published byTracy Williamson Modified over 8 years ago
1
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers
2
© M. Gross, ETH Zürich, 2014 Agenda Pointers Defining pointers Reference operator & Dereference operator * Dynamic memory allocation new and delete Pointers and arrays Pointer arithmetic Dynamic array allocation 2
3
© M. Gross, ETH Zürich, 2014 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
4
© M. Gross, ETH Zürich, 2014 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;
5
© M. Gross, ETH Zürich, 2014 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
6
© M. Gross, ETH Zürich, 2014 Pointers: Syntax Don’t confuse * with * 6 int a, b; int *p; p = &a; *p = 5; b = *p; pointer declaration dereference operator
7
© M. Gross, ETH Zürich, 2014 Pointers: Example 7 Memory address 0x1000 0x1004 0x1008 0x100C 0x1010 int a, b; a b
8
© M. Gross, ETH Zürich, 2014 Pointers: Example 8 Memory address 0x1000 0x1004 0x1008 0x100C 0x1010 int a, b; a = 12; b = 5; a b 12 5
9
© M. Gross, ETH Zürich, 2014 Pointers: Example 9 Memory address 0x1000 0x1004 0x1008 0x100C 0x1010 int a, b; a = 12; b = 5; int *p; a b 12 5 p
10
© M. Gross, ETH Zürich, 2014 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
11
© M. Gross, ETH Zürich, 2014 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
12
© M. Gross, ETH Zürich, 2014 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 p 0x1000
13
© M. Gross, ETH Zürich, 2014 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
14
© M. Gross, ETH Zürich, 2014 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
15
© M. Gross, ETH Zürich, 2014 Pointers 15
16
© M. Gross, ETH Zürich, 2014 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
17
© M. Gross, ETH Zürich, 2014 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;
18
© M. Gross, ETH Zürich, 2014 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
19
© M. Gross, ETH Zürich, 2014 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
20
© M. Gross, ETH Zürich, 2014 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; 00000 00004 01004 02004
21
© M. Gross, ETH Zürich, 2014 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)
22
© M. Gross, ETH Zürich, 2014 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;
23
© M. Gross, ETH Zürich, 2014 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 vorname name << endl; cout legi << endl; delete stud1;// Free memory
24
© M. Gross, ETH Zürich, 2014 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.