© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Pointers OVERVIEW.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Fundamentals of C++ Yingcai Xiao 09/03/08. Outline Class Definition IO Template vector C Pointer Dynamic Memory Allocation.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Review 1 List Data Structure List operations List Implementation Array Linked List.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Pointers *, &, array similarities, functions, sizeof.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Student Book An Introduction
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers

© 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

© 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

© 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;

© 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

© 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

© M. Gross, ETH Zürich, 2014 Pointers: Example 7 Memory address 0x1000 0x1004 0x1008 0x100C 0x1010 int a, b; a b

© 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

© 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

© 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

© 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

© 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

© 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 p 0x1000

© 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 p 0x1000

© M. Gross, ETH Zürich, 2014 Pointers 15

© 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

© 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;

© 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

© 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

© 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;

© 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)

© 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;

© 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

© 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