CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

Similar documents
CS 31 Discussion ABDULLAH-AL-ZUBAER IMRAN WEEK 6: C-STRINGS, STRUCT, CLASS AND PROJECT4

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

CS31 Discussion 1E Spring 17 : week 08

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

Homework #3 CS2255 Fall 2012

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

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

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

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Input And Output of C++

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

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

Pointers, Dynamic Data, and Reference Types

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

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

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

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

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

Exercise 1.1 Hello world

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

FORM 2 (Please put your name and form # on the scantron!!!!)

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

Week 8. Muhao Chen

Pointers and References

Computer Science II Lecture 1 Introduction and Background

CS302 - Data Structures using C++

CS2255 HOMEWORK #1 Fall 2012

Review Questions for Final Exam

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Introduction to Scientific Computing and Problem Solving

CS201- Introduction to Programming Current Quizzes

Creating a C++ Program

Review of Important Topics in CS1600. Functions Arrays C-strings

Linked List using a Sentinel

Pointers, Arrays and C-Strings

CSC 1300 Exam 4 Comprehensive-ish and Structs

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Getting started with C++ (Part 2)

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

UNDEFINED BEHAVIOR IS AWESOME

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

C++ for Java Programmers

Programming in C++: Assignment Week 2

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Fast Introduction to Object Oriented Programming and C++

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

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

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

Introduction to Programming

Lecture on pointers, references, and arrays and vectors

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Unit IV & V Previous Papers 1 mark Answers

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

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

VARIABLES & ASSIGNMENTS

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

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

CS24 Week 3 Lecture 1

Bruce Merry. IOI Training Dec 2013

1 Short Answer (5 Points Each)

Intermediate Programming, Spring 2017*

CSC 211 Intermediate Programming. Arrays & Pointers

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

Reference operator (&)

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

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

CS 61c: Great Ideas in Computer Architecture

Chapter 2: Basic Elements of C++

6. Pointers, Structs, and Arrays. 1. Juli 2011

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

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

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

A First Program - Greeting.cpp

CS242 COMPUTER PROGRAMMING

Object Reference and Memory Allocation. Questions:

Chapter 2. Procedural Programming

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Assignment #1 (50 points; due 11:59 P.M.)

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Introduction to Programming I COS1511 School of Computing Revision Notes

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.

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Exam 3 Chapters 7 & 9

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Transcription:

CS31 Discussion Jie(Jay) Wang Week8 Nov.18

Outline Pointer Struct

Memory Management When the program gets executed, it gets some amount of memory allocated for use. memory Program 1 Program 2

Memory Management Consider this program int main() { func1(); // call func1() void func1() {... func2(); // call func2() main() some data main() some data func1() main() some data func1() main() some data func2() func1() main() some data

Memory Management Every variable you create during the program execution gets its own space in some location within the memory. And every location is marked with a unique address. int x = 16; char c[] = cat 16 1000 1001 1002 1003 1004 1005 1006 c a t \0 1000 1001 1002 1003 1004 1005 1006

Pointers The address-of operator (&): get the memory address of the expression to the right of the ampersand. int x = 16; cout << &x << endl; 16 1000 1001 1002 1003 1004 1005 1006

Pointers Pointers store memory addresses and are assigned a type corresponding to the type of the variable that they point to. <type>* <name> // declares a pointer of the given <type> and calls it <name>. int* ptrage; bool* ptrvalid; char* ptrname; To initialize pointers int age = 40; int* prtage = &age; or int* ptrage; ptrage = &age; 16 1000 1001 1002 1003 1004 1005 1006 int* prtage = &age;

Pointers The dereference operator (*): to dereference a pointer to get the variable pointed by the pointer. #include <iostream> using namespace std; int main() { double x, y; // normal double variables double *p; // a pointer to a double variable x = 5.5; y = -10.0; p = &x; // assign x s memory address to p (make p point to x) cout << "p: " << p << endl; cout << "*p: " << *p << endl; p = &y; cout << "p: " << p << endl; cout << "*p: " << *p << endl; return 0; x 5.5 x -10.0 p

Pointers Question: Will the code compile? If so, what s the output? #include <iostream> using namespace std; int main(){ int *ptr; cout << *ptr << endl; Be careful! Uninitialized pointers can lead to undefined behavior or illegal memory accesses when they haven't been assigned somewhere first. A special keyword nullptr that represents the pointer that points at nothing.

Pointers Question: Will the code compile? If so, what s the output? #include <iostream> using namespace std; int main(){ int *ptr = nullptr; cout << *ptr << endl; Attempting to dereference a nullptr will result in undefined behavior.

Pointers We can check to make sure a pointer is or is not null pointer. #include <iostream> #include <string> using namespace std; int main () { int i = 50; int* latepointer = nullptr; if (latepointer == nullptr) { latepointer = &i; else { cout << "<_< >_>" << endl; cout << *latepointer << endl;

Pointers and Arrays int arr[100]; arr is actually a pointer(int*) Special for arr, the pointee can t change. In order to get the value of arr[1] arr[1] *(arr+1)

Pointers and Arrays Question: Will the code compile? If so, what s the output? #include <iostream> using namespace std; int main(){ int arr[100]; int var = 100; for (int i = 0; i < 100; i++) arr[i] = i; cout << *(arr+1) << endl; cout << *(&arr[1]) << endl; *arr = var; cout << arr[0] << endl;

Pointers and Arrays Question: Will the code compile? If so, what s the output? #include <iostream> using namespace std; int main(){ int arr[100]; int var = 100; for (int i = 0; i < 100; i++) arr[i] = i; cout << *(arr+1) << endl; cout << *(&arr[1]) << endl; arr = &var; cout << arr[0] << endl; var 100 What about arr[1]? arr+1 =??? Array elements are located contiguously in memory.

Pointer Arithmetic #include <iostream> using namespace std; int main(){ int arr[100]; int var = 100; for (int i = 0; i < 100; i++) arr[i] = i; cout << arr+1 << endl; cout << arr+2 << endl; arr is pointing to the integer. A integer is of 4 bytes on this platform.

About sizeof(int) The only things guaranteed are: The other things are implementation defined. http://stackoverflow.com/questions/13398630/why-are-c-int-and-long-types-both-4-bytes

Pointer Arithmetic Subtraction and addition to pointers is well defined, such that if I say (ptr + i), it means "refer to the address i times x bytes away from ptr," where x is the size of the type of ptr. Pointers are NOT defined on multiplication or division! #include <iostream> #include <string> using namespace std; int main () { double d[] = {1.1, 2.2, 3.3, 4.4, 5.5; double* ptr = d; cout << *(ptr * 2) << endl;

Pointers and Arrays You can treat an array variable like a pointer well, it is a pointer. Therefore, the following are equivalent: int findfirst(const string a[], int n, string target); int findfirst(const string* a, int n, string target); Recall Pass by value int foo(int n); Pass by reference int foo(int &n); Pass by pointer int foo(int a[]); int foo(int* a);

Program Challenge The following is one possible implementation of findfirst(). Can you modify it such that it doesn t use brackets? int findfirst(const string a[], int n, string target) { for (int i = 0; i < n; i++) if (a[i] == target) return i; return -1; int findfirst(const string* a, int n, string target) { string a[5] = {"home", "marge", "bart", "marge", "lisa"; cout << findfirst(a, 5, "marge") << endl; cout << findfirst(a + 2, 3, "marge") << endl;

Pointer to pointer int** var; int** int* int address address value #include <iostream> using namespace std; int main() { int var; int *ptr; int **pptr; var = 3000; ptr = &var; pptr = &ptr; cout << "Value of var = " << var << endl; cout << "Value available at *ptr = " << *ptr << endl; cout << "Value available at **pptr = " << **pptr << endl;

Reference to Pointer int* &ptr; #include <iostream> using namespace std; int main() { int var1 = 30; int var2 = 50; int* ptr1 = &var1; int* &ptr2 = ptr1; cout << *ptr1 << endl; ptr2 = &var2; cout << *ptr1 << endl; var1 30 var2 50 (ptr2) ptr1

Why do we need them? #include <iostream> int g_n = 42; void func_ptr(int* pp) { pp = &g_n; int main() { int n = 23; int* pn = &n; std::cout << "Before :" << *pn << std::endl; func_ptr(pn); std::cout << "After :" << *pn << std::endl; Question: how can I get 42 after calling the function?

Solution1: Pointer to Pointer #include <iostream> int g_n = 42; void func_ptr(int** pp) { *pp = &g_n; int main() { int n = 23; int* pn = &n; std::cout << "Before :" << *pn << std::endl; func_ptr(&pn); std::cout << "After :" << *pn << std::endl; n 23 pn pp g_n 42

Solution2: Reference to Pointer #include <iostream> int g_n = 42; void func_ptr(int* &pp) { pp = &g_n; int main() { int n = 23; int* pn = &n; std::cout << "Before :" << *pn << std::endl; func_ptr(pn); std::cout << "After :" << *pn << std::endl; n 23 pn (pp) g_n 42

Preference of one over the other? Now we have seen the syntax of ptr-to-ptr and refto-ptr. Are there any advantages of one over the other? I am afraid, no. The usage of one of both, for some programmers are just personal preferences. Some who use ref-to-ptr say the syntax is "cleaner" while some who use ptr-to-ptr, say ptr-to-ptr syntax makes it clearer to those reading what you are doing. http://www.codeproject.com/articles/4894/pointer-to-pointer-and- Reference-to-Pointer

Programming Challenge Define a function, ptrstominmax that takes in an arrays of ints, the size of that array, and two int pointers, and then sets each pointer equal to the address holding the minimum and maximum values within that array. #include <iostream> #include <string> #include <cassert> using namespace std; void ptrtominmax (int arr[], int*& min, int*& max, int n); int main () { int arr[] = {1, 5, 0, 2, 4; int* min; int* max; ptrtominmax(arr, min, max, 5); assert(*min == 0); assert(*max == 5); cerr << "[!] ALL TESTS PASSED!" << endl; void ptrtominmax (int arr[], int*& min, int*& max, int n) { // Simply return if n is 0 or below if (n <= 0) { return; // [!] We'll start off both of our min max pointers // at the first element min =??? max =??? // Iterate through all n elements of arr for (int i = 0; i < n; i++) { // [!] If the current element is less than the min // we'll set the min to that element if (??? ) { min =??? // [!] If the current element is greater than the // max, we'll set the max to that element if (??? ) { max =???

Struct Structs are objects in C++ that represent "data structures", or variables, functions, etc. that are organized under a categorizing identifier. Data Members are variable components of a given struct; they can be of any variable type. struct <structname> { <member1_type> <member1_name>; <member2_type> <member2_name>; //...etc. ; // Remember the semicolon! struct Student { string name; int id; string email; char grade; ;

Struct struct Student { string name; int id; string email; char grade; ; const int NUM_STUDENTS = 32; Student st Student students[num_students]; The element/member selection operation (.) st.name = "Joe Bruin"; // st s name is set to Joe Bruin students[10].id = 123456789; // the 10-th Student in students array is assigned an ID cout << st.grade << endl; // print the grade of st cout << students[0].email << endl; // print the 0-th Stduent s email address

Pointers to Structures student *p; You can refer to a member of the structure pointed by p by first dereferencing it and using the dot operator. (*p).name Or p->name This one works only if p is a pointer.

Constructors and Initializing Structs A constructor is used to initialize the data members of a newly declared struct object; it's how we "initialize" them. Constructors are called automatically at struct instance declaration and are defined by listing a member function with the same name as the struct and NO return type. StructName () { /* Constructor Body */ A parameterized constructor is simply a constructor that takes parameters when constructing an object. StructName (paramtype1 param1, paramtype2 param2,...) { /* Constructor Body */

Constructors and Initializing Structs Student { string name; int id; string email; char grade; Student() { name = "Jane Doe"; id = 0; email = "aa@a.com"; grade = 'A'; Student(string n, int i, string e, char g) { name = n; id = i; email = e; grade = g; ; int main() { Student p; cout << p.name << endl; cout << p.grade << endl; Student q("simon", 12, "simon@ht.com", 'B'); cout << q.name << endl; cout << q.grade << endl;

Member Functions (Method) #include <iostream> #include <string> using namespace std; struct Student { string name; int id; string email; char grade; Student() { name = "Jane Doe"; id = 0; email = "aa@a.com"; grade = 'A'; void printname() { cout << name << endl; ; #include <iostream> #include <string> using namespace std; struct Student { string name; int id; string email; char grade; Student() { name = "Jane Doe"; id = 0; email = "aa@a.com"; grade = 'A'; ; void Student::printName() { cout << name << endl; int main() { Student p; p.printname();

What s the output? #include <iostream> #include <cstring> #include <cctype> #include <string> using namespace std; struct trickz { int i[5]; trickz () { for (int j = 0; j < 5; j++) { // Assume ASCII encoding i[j] = j; ; void trickzinc (trickz*& t, int count) { for (int j = 0; j < count; j++) { t->i[j]++; ; int main () { trickz t; trickz* ptr = &t; t.trickzinc(ptr, 5); for (int j = 0; j < 5; j++) { cout << t.i[j] << endl; // True or false? cout << (ptr == &t) << endl;

Credit to 2 previous CS31 TAs This slide is finished with reference from: Andrew Forney http://web.cs.ucla.edu/~forns/ Brian Choi http://netlab.cs.ucla.edu/~schoi/cs31/

Thanks!