C++ for Java Programmers

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

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

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

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

QUIZ. What is wrong with this code that uses default arguments?

Homework #3 CS2255 Fall 2012

Pointers in C/C++ 1 Memory Addresses 2

C++ for Java Programmers

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/


Pointers. Variable Declaration. Chapter 10

C++ Programming Chapter 7 Pointers

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

Pointers and References

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

Chapter 11: Pointers

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

CS61C : Machine Structures

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

DECLARAING AND INITIALIZING POINTERS

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

HW1 due Monday by 9:30am Assignment online, submission details to come

CS 61c: Great Ideas in Computer Architecture

Pointer Data Type and Pointer Variables

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

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

First of all, it is a variable, just like other variables you studied

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.

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Ch. 11: References & the Copy-Constructor. - continued -

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

High Performance Computing MPI and C-Language Seminars 2009

Pointers, Dynamic Data, and Reference Types

Dynamic Allocation in C

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

12. Pointers Address-of operator (&)

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Ch. 12: Operator Overloading

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

pointers + memory double x; string a; int x; main overhead int y; main overhead

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Introduction to C: Pointers

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CS61C : Machine Structures

Linked lists Tutorial 5b

Sixth lecture; classes, objects, reference operator.

Reference operator (&)

Pointers. 10/5/07 Pointers 1

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CMSC202 Computer Science II for Majors

Lecture 2, September 4

by Pearson Education, Inc. All Rights Reserved.

Chapter 1 Getting Started

[0569] p 0318 garbage

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

QUIZ. What are 3 differences between C and C++ const variables?

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

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

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Introduction to Scientific Computing and Problem Solving

C Pointers. 7.2 Pointer Variable Definitions and Initialization

CS61C : Machine Structures

Lecture 2: C Programm

Pointers and Strings Prentice Hall, Inc. All rights reserved.

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Operator overloading

PROGRAMMAZIONE I A.A. 2017/2018

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CS 2461: Computer Architecture I

These are notes for the third lecture; if statements and loops.

CS3157: Advanced Programming. Outline

CS201- Introduction to Programming Current Quizzes

What is an algorithm?

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

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

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Chapter 1: Object-Oriented Programming Using C++

CS61C : Machine Structures

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

Intro. Scheme Basics. scm> 5 5. scm>

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Transcription:

Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even more on Pointers Many problems can only be solved by representing a significant amount of information. C/C++ provide us with mechanisms for dynamic memory allocation and management of data structures during runtime. This means we can write flexible programs where sizes of data structures don t have to be set at compile time. 4 Java uses exactly the same mechanisms in the background but hides them from you. Dynamic Structures LValues and Rvalues Access to these Dynamic structures are through pointers. In C++ there are two kinds of expressions LVALUES RVALUES a pointer is just a variable whose value is the location of another object. Those that represent objects that can be modified and evaluated Those that represent objects which can only be evaluated. 5 C++ provides two fundamental pointer operators: the address operator & the dereferencing operator * 6 Although assignment typically uses an object name as its left operand, the syntax only requires it to be an LVALUE. This is important because not all objects actually have names. (eg. elements in an array). 1

Quick Example Pointer Basics #include <iostream.h> int main () int a = 1; int b; int c[]; A pointer is a variable whose value represents the location of another object. Pointer objects are defined in conjunction with the unary indirection operator *. (also known as the dereferencing operator) lvalues b=5; cout << b << endl; c[0] = 2*a; return 0; rvalues Lets set up three pointers: int *iptr; char *cptr; float *fptr; 7 8 Null Pointers Pointer Representation So what is in these pointers at the moment? who knows - they are all uninitialised. This is bad bad bad. iptr? iptr There is one literal that we can assign to any type of pointer. This is the value 0, which in this context is known as the address: cptr fptr?? cptr fptr int *iptr = 0; char *cptr = 0; float *fptr = 0; Uninitialised Pointers Null Pointers 9 10 Pointer Assignment Example of Mistakes The assignment = is fine for use with pointer objects, so long as the pointers are of the same type. Ptr1 = Ptr2; This will result in Ptr1 and Ptr2 pointing to the same object. There is still only one object, whatever it is - it just has two things pointing at it. int i = 1; int *iptr; char *cptr; iptr = i; i = iptr; iptr = cptr; illegal: i is not an int*. We ve used a pointer as the right operand here, but it could have been any valid expression. illegal: iptr is not an int. illegal: cptr is not an int* 11 12 2

Quick BUG highlight The Address Operator Some people juxtapose the * so the statement reads more logically: char* cptr; But it can lead to a misunderstanding. For example: char* a, b; really. char* a; char b; & is the addressing operator It returns the memory address of a variable in pointer form. Take the following code segment: int number = 1; int *Ptr; Ptr = &number; number 1 1 Hence this statement does not lead to the creation of two pointers. Always define only one pointer per statement. 14 sets pointer to the memory location for number Ptr Dereferencing Initialising Pointers 15 The value for number can now be accessed directly or indirectly. The indirect uses the dereferencing pointer (the * operation computes the lvalue of the object at which the pointer is aimed). cout << *Ptr << endl; Hence we can also modify the number variable by: *Ptr = 99; 16 Remember if you don t know where a pointer is heading yet it s a good idea to initialise it as. However pointers can include various initialisation expressions: int a = ; int b = 7; int *Ptr1 = &b; int *Ptr2 = Ptr1; int *Ptr = &a; a b 7 Ptr1 Ptr2 Ptr More assignment. What can you point at? What would happen if the following code was then also executed? *Ptr1 = *Ptr; Ptr2 = Ptr; a b Ptr1 7 practically anything: variables your own types arrays structs instances of classes even functions. 17 Ptr2 Ptr 18 essentially any sort of data we can store in memory.

Pointers to Pointers Pointers to Pointers C++ allows pointer types whose objects are pointers to other pointers. Or even pointers to pointers to pointers The following is a definition of a pointer to an int* object: int **PtrPtr1; Consider the following example: int num = 25; int *Ptr = &num; int **PtrPtr = 0; PtrPtr = &Ptr; PtrPtr Ptr Each asterisk in a definition indicates another level of dereferencing num 25 19 20 Pointer Power Constants and Pointers The ability to access and change the value of something whose name is not part of the assignment statement makes pointer programming powerful but confusing The modifier const can be applied to pointer declarations. However where you place this keyword can give the statement a drastically different meaning 21 Errors involving pointers are often subtle and hard to track down A simple technique is to draw your own diagrams this can be so useful even experienced professional C++ coders use this technique to help debug code. 22 const char *Ptr1 = &ch; char const *Ptr2 = &ch; char *const Ptr = &ch; Mean the same Thing the pointer is not a constant, but what its pointing at is. Here the opposite. The pointer is a constant, the char is not. Constants and Pointers Program using Pointers We could even have: #include <iostream.h> const char *const Ptr4 = &ch; The C++ syntax can make understanding these declarations difficult. Solution read the declaration backwards replacing * with is a pointer to. void Swap(char *Ptr1, char *Ptr2) char c = *Ptr1; *Ptr1 = *Ptr2; *Ptr2 = c int main() char a = y ; char b = n ; Swap(&a,&b); cout << a << b << endl; 2 24 4

Pointers as Parameters Arrays of Pointers 25 The above program shows that using pointers we can mimic passing by reference. To us C++ programmers this seems as though we are just replicating what we did using the & operator to pass by reference. However to a C programmer this is crucial because C doesn t provide standard reference parameters. Life is easier for a C++ coder 26 Because a pointer is just a type of variable (i.e. one that stores a memory address) it can often be treated just like you would any other variable. Hence you can quite happily created an array of pointers if you so wish. int* Ptr4[5]; This code for example would set up an array of five pointers to integers. Pointers to Arrays Example 27 Never confuse an array of pointers with a pointer to an array! In C++ the name of an array is considered to be a constant pointer. The name of the array is associated with a particular memory location, and this association cannot be changed In fact it is associated with the location of the first element in the array. 28 int ant[5]; int dec[7]; int *Ptr1 = ant; int *Ptr2 = &ant[0]; int *Ptr = dec; int *Ptr4 = &dec[4]; Equivalent statements Ptr points at dec[0] Ptr points at dec[4] Whats going on? That Array in Main Say we had: int myarray[4]=10,20,0,40; int *ptr = myarray; To take arguments in from the command line we used: int main(int argc, char *argv[]) arg[0] arg[1] arg[2] myarray ptr argc arg 10 20 0 40 c m d \0 j i m \0 2 5 \0 29 0 5

Pointer Arithmetic Pointer Arithmetic Example 1 There are only two arithmetic operations that you can perform on pointers: addition and subtraction. Hence all of the following expressions are valid: Ptr++; Ptr--; Ptr2 = Ptr1-2; Ptr = (&x) + 4; Best use an example to explain this so here goes: 2 Say we have a integer pointer Ptr, withacurrent value (which of course is a memory address) of 2000. Now we have the expression: Ptr++ What s the value of Ptr now? You might expect it be 2001 but it is in fact 2004. This is because when the pointer is incremented it moves to the next int held in memory 4 bytes across. In general Pointer Arithmetic Program Each time a pointer is incremented it points to the memory location of the next element of its base type. ptr 2000 #include <iostream.h> int main() int cubes[] = 1,4,9,16,25,6; int *ptr; ptr+1 ptr+2 ptr+ ptr+4 2004 2008 2012 2016 ptr = cubes; for(int i=0; i<6; i++) cout << *(Ptr + i) << endl; 4 The Indexing Deception Character String Processing So what does this mean? It means these are the same int array[] = 1,,5; cout << array[0]; cout << array[1]; cout << array[2]; int[]* array = 1,,5; cout << *(array); cout << *(array+1); cout << *(array+2); All this is particularly for our good old terminated strings (remember them?) Null terminated strings are of course character arrays. The name of the string is just a pointer to the first letter. These are identical apart from the pointer array in the left hand code is a constant. On the right it is not. We can use pointer arithmetic to manipulate arrays and that s exactly what the functions in <iostream> are doing. 5 6 6

Interchangeability Pointers to Functions 7 The interchangeability of pointer and array notation is most obvious when you pass char strings to functions. Both of the following are possible implementations of the strlen library function: int strlen(const char s[]) int i for(i=0; s[i]!= \0 ; ++i); return i int strlen(const char *s) int i; for(i=0; *s++!= \0 ; ++i); return i 8 Earlier we said that you can use a pointer to point to almost anything even functions. The primary use of this is as a parameter to another function. This allows the destination function the ability to invoke a variety of actions Just like an array name points to its elements, a function name is a pointer to the location of the code that constitutes its actions. Pointers to Functions So what is the point? For example, when we call FuncPtr here it is the same as calling toupper: int (*FuncPtr)(int i); FuncPtr = toupper; Well we have: an alternative notation for manipulating arrays a way to access command line arguments is that it? 9 You could use this characteristic to send a certain function: void Display(char word[], int (*FuncPtr)(int i)) dostuff word = (*FuncPtr)(word); 40 No Sireee. The main role of pointers is for creating dynamic objects. Dynamic Memory allows to solves lots of problems that might otherwise be intangible Problems with Pointers Tomorrow Wild pointers are the devils work. Pointers give you tremendous power, but if one accidentally contains the wrong value they can be a bugger to find. Take as much care as you possibly can not to make pointer errors. Classic example is the unitialised pointer. We ve pretty much covered the fundamentals of pointers now you ve just got to learn how to apply them Tomorrow we ll be looking at Dynamic Memory Allocation the real role of pointers. Extra lab has been organised for Friday at 4pm just for those digital business students who have a clash. 41 42 7