Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Similar documents
Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Intermediate Programming & Design (C++) Classes in C++

Pointers. Memory. void foo() { }//return

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Algorithms & Data Structures

Vector and Free Store (Pointers and Memory Allocation)

Pointers! Arizona State University 1

CS201- Introduction to Programming Current Quizzes

Homework #3 CS2255 Fall 2012

DECLARAING AND INITIALIZING POINTERS

Dynamic Allocation of Memory

[0569] p 0318 garbage

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CA31-1K DIS. Pointers. TA: You Lu

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Object-Oriented Principles and Practice / C++

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Chapter 17 vector and Free Store

C Pointers. 6th April 2017 Giulio Picierro

In Java we have the keyword null, which is the value of an uninitialized reference type

Pointers and Memory 1

Exam 3 Chapters 7 & 9

Week 3: Pointers (Part 2)

REFERENCES, POINTERS AND STRUCTS

Pointers, Dynamic Data, and Reference Types

Chapter 17 vector and Free Store

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Procedural programming with C

Chapter 17 vector and Free Store. Bjarne Stroustrup

What is an algorithm?

EL2310 Scientific Programming

Object-Oriented Principles and Practice / C++

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

Variation of Pointers

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

C++ for Java Programmers

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Introduction to Scientific Computing and Problem Solving

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Pointers and Dynamic Memory Allocation

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Lecture Topics. Administrivia

And Even More and More C++ Fundamentals of Computer Science

Pointers. Pointers. Pointers (cont) CS 217

POINTER & REFERENCE VARIABLES

CSC1322 Object-Oriented Programming Concepts

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

Object-Oriented Principles and Practice / C++

04-19 Discussion Notes

Pointers and References

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

What have we learned about when we learned about function parameters? 1-1

Short Notes of CS201

Chapter 2. Procedural Programming

Classes and Objects. Class scope: - private members are only accessible by the class methods.

CS201 - Introduction to Programming Glossary By

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

MYcsvtu Notes LECTURE 34. POINTERS

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

The vector Class and Memory Management Chapter 17. Bjarne Stroustrup Lawrence "Pete" Petersen Walter Daugherity Fall 2007

Abstract. Vector. Overview. Building from the ground up. Building from the ground up 1/8/10. Chapter 17 vector and Free Store

Solution Manual for Data Structures and Problem Solving Using C++ 2nd edition by Mark A. Weiss

CS 161 Exam II Winter 2018 FORM 1

Class Information ANNOUCEMENTS

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Dynamic Allocation of Memory

Lecture 05 POINTERS 1

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays


Summer May 18, 2010

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Variables, Memory and Pointers

Lecture Notes on Memory Layout

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Pointers. Variable Declaration. Chapter 10

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Arrays and Pointers in C. Alan L. Cox

Arrays, Pointers and Memory Management

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Tokens, Expressions and Control Structures

CSC 211 Intermediate Programming. Arrays & Pointers

Structures and Pointers

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Transcription:

Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to. Teresa Leyk Slide 1 C++ Review Pointers

Pointers Pointers are used to: create more complex data types such as lists, queues, stacks or trees process arrays keep track of allocated memory returned by the new operator deallocate a block of memory by the delete operator Teresa Leyk Slide 2 C++ Review Pointers

Pointers (cont) To define a pointer variable we use the same rules as defining an ordinary variable. The only difference is that the asterisk is added in front of the name of the pointer variable. int i = 1; // define and initialize a variable i int *iptr; // define a pointer iptr iptr = &i; /* now value of iptr is the same as the address of i */ *iptr = 2; /* stores a value (2) in a variable pointed by iptr */ *iptr = *iptr + 1; // a new value of i is 3 The operator & is the address-of operator. The asterisk in *ptr is called the dereferencing (or indirection) operator. Teresa Leyk Slide 3 C++ Review Pointers

Pointers (cont) Pointers can point to nothing (NULL or 0) or to allocated memory by using the operator new. The operator new dynamically allocates the correct amount of memory for an object of a given type (from the free store) and returns a pointer to this object. Allocated memory can be returned to the free store by the operator delete. struct Student { }; int studentid; double studentgrade; Student *P = new Student; // a pointer to a structure P->studentID = 691001221; // initialize // (*P).studentID = 691001221; an equivalent notation P->studentGrade = 3.5; // initialize // (*P).StudentGrade = 3.5; is an equivalent notation delete P; // deallocate Teresa Leyk Slide 4 C++ Review Pointers

Pointers (cont) Computer memory is a one-dimensional array of bytes. Each object is stored in a contiguous sequence of bytes. The address of an object is the address of the first byte in this sequence. Let int *iptr = &num;. Then iptr value (address) 123200 refers to the location of the integer variable num and *iptr has the value 2. 12800 123200 address 123200... 2... data int *iptr int num object type To avoid run-time errors with pointers a programmer needs to remember to initialize them either to 0 or to an address of an existing object. Pointers in functions are not automatically initialized to 0. Teresa Leyk Slide 5 C++ Review Pointers

Pointers and References A reference is an automatically dereferenced pointer, or an alternative name for an object. Differences between pointers and references: to generate a pointer value you use the operator new or address-of (&) a pointer s value (address) can be changed assignment of pointers does shallow copy (only addresses are copied) int i = 1; int *pi = &i; int *pa = new int(10); pa = pi; // shallow copy to access an object pointed to by a pointer you use the operators * or [] *pi = 2; pi[0] = 3; Teresa Leyk Slide 6 C++ Review Pointers

Pointers and References (cont) a reference value (address) cannot be changed after its initialization int &ri = *pi; // now ri is an alias of *pi ri = pa; // illegal (ri is not a pointer) ri = *pa; // legal (*pa is a variable) a reference cannot be initialized to 0 int &rb = *0; // illegal pi = 0; // legal assignment of references does deep copy int j = 5; int i = 10; int &rj = j; int &ri = i; rj = ri; // value of j is now 10 Teresa Leyk Slide 7 C++ Review Pointers

Pointers and References (cont) The references are practically used only in function arguments (call-by-reference) and for returning an object from a function as a reference. Example. // swap values of x and y void swap(int& x, int& y) { } int tmp = x; x = y; y = tmp; Teresa Leyk Slide 8 C++ Review Pointers

The this Pointer The keyword this identifies a special type of pointer. Suppose that you create an object named g of class B, and class B has a member function funct(). If you call the function g.funct(), the keyword this in the body of funct() stores a pointer to (address of) g. You cannot declare the this pointer or make assignments to it (it is a constant pointer). In other words, the this pointer is a pointer that points to the object for which the member function is called. The this pointer is called implicitly every time we access a class member (that s why you do not see a lot of this in C++ programs). Only when we need to refer to the whole object, we need to mention this explicitly. Teresa Leyk Slide 9 C++ Review Pointers

The this Pointer (cont) Example (see pages 603 607). class Link { public: string value; Link(const string& v, Link *p=0, Link *s=0) : value(v), prev(p), succ(s) {} // constructor Link* insert(link* n); // insert n before this object // more Link functions are here private: Link *prev, *succ; }; Teresa Leyk Slide 10 C++ Review Pointers

The this Pointer (cont) Example (cont) // insert n before this object Link* Link::insert(Link* n) { } if (n==0) return this; n->succ = this; // this object comes after n if (prev!=0) prev->succ = n; n->prev = prev; prev = n; return n; Note that we could have used this->prev and this->succ in place of prev and succ, respectively, but it is not necessary, and it is not considered as a good programming style. Teresa Leyk Slide 11 C++ Review Pointers

The this Pointer (cont) Example (cont). It is how we invoke the insert function. Link* greek_gods = new Link("Hera"); // it inserts the string "Athena" before "Hera" greek_gods = greek_gods->insert(new Link("Athena")); // it inserts the string "Mars" before "Athena" greek_gods = greek_gods->insert(new Link("Mars")); // it inserts the string "Poseidon" before "Mars" greek_gods = greek_gods->insert(new Link("Poseidon")); The this pointer inside the insert function has the same value as the pointer greek_gods. Note that the pointer greek_gods is changed in each corresponding line so the this pointer is different in each call. Teresa Leyk Slide 12 C++ Review Pointers

Generic (type void*) Pointer The type void* pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type. A generic pointer is declared like a normal pointer, using the void keyword as the pointer s type: void* pgen; // pgen is a generic pointer A generic pointer can point to objects of any data type. The type void* means pointer to some memory that the compiler does not know the type of. We use void* when we want to transmit an address between pieces of code that really do not know each other s types. Examples: the address arguments of a callback function and lowest level of memory allocation (used in the implementation of the new operator). Teresa Leyk Slide 13 C++ Review Pointers

Generic (type void*) Pointer (cont) A pointer to any object type can be assigned to a void*. Examples: void* pv1 = new int; // int* converts to void* void* pv2 = new double[10]; // double* converts to void* When we want to access a value pointed by a generic pointer, we need to cast it to correct type (we need to know which type, otherwise the program will crash). Examples: int i = 10; void* pv = &i; // int* converts to void* int* pi = static_cast<int*>(pv); // explicit conversion *pi = -1; // the value of i is now -1 *pv = -1; // ERROR: cannot dereference a void* Teresa Leyk Slide 14 C++ Review Pointers